微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EmbedMany表格

如何解决EmbedMany表格

| 我已经创建了一个小型的Symfony2-Web站点(使用Symfony PR11),并将MongoDB作为数据库。 我可以创建一个使用普通文档的表单,但是如何使用带有EmbeddedDocument的文档? 以下是文件
/**
 * @mongodb:Document(collection=\"location\")
 */
class Location
{
    /**
     * @mongodb:Id
     */
    protected $id;

    /**
     * @mongodb:String
     */
    protected $locationName;

    /**
     * @mongodb:EmbedMany(targetDocument=\"LocationTerminal\")
     */
    protected $terminals = array();

    // Setter

    public function setTerminals(LocationTerminal $terminal)
    {
        array_push($this->terminals,$terminal);
    }

    public function setLocationName($locationName)
    {
        $this->locationName = $locationName;
    }


    // Getter

    public function getId()
    {
        return $this->$id;
    }

    public function getLocationName()
    {
        return $this->locationName;
    }

    public function getTerminals()
    {
        return $this->terminals;
    }

}
EmbeddedDocument:
/**
 * @mongodb:EmbeddedDocument
 */
class LocationTerminal
{
    /**
     * @mongodb:String
     */
    protected $terminalName;

    /**
     * @mongodb:Int
     */
    protected $since;

    /**
     * @mongodb:Int
     */
    protected $to;

    // Setter

    public function setTerminalName($terminalName)
    {
        $this->terminalName = $terminalName;
    }

    public function setSince($since)
    {
        $this->since = $since;
    }

    public function setTo($to)
    {
        $this->to = $to;
    }

    // Getter

    public function getTerminalName()
    {
        return $this->terminalName;
    }

    public function getSince()
    {
        return $this->since;
    }

    public function getTo()
    {
        return $this->to;
    }

}
如您所见,
$terminals
持有an3ѭ-文档 形式如下:
class LocationForm extends Form
{
    public function configure()
    {
        $this->add(new TextField(\'locationName\',array(\'max_length\' => 255,\'required\' => true)));
    }

    public function addTerminals($dm)
    {
        $this->add(new ChoiceField(\'terminals.terminalName\',array(\'choices\' => $dm)));
        $this->add(new DateField(\'terminals.since\',array(\'required\' => true)));
        $this->add(new DateField(\'terminals.to\',array(\'required\' => false)));
    }
}
使用的控制器如下所示:
class LocationController extends Controller
{
    protected $location;
    protected $locationTerminal;

    protected function getDm()
    {
        return $this->get(\'doctrine.odm.mongodb.document_manager\');
    }

    protected function getLocation($name = null)
    {
        if ($name != null)
        {
            $dm = $this->getDm();
            $this->location = $dm->getRepository(\'RalfBundle:Location\')->findOneBy(array(\'locationName\' => $name));
            if (! $this->location)
            {
                $this->location = new Location();
                $this->locationTerminal = new LocationTerminal();
                $this->location->setLocation($name);
                $this->location->setTerminals($this->locationTerminal);
            }
        }
        else
        {
            $this->location = new Location();
            $this->locationTerminal = new LocationTerminal();
            $this->location->setTerminals($this->locationTerminal);
            $this->locationTerminal->setSince(0);
            $this->locationTerminal->setTerminalName(\"\");
            $this->locationTerminal->setTo(0);
        }
    }


protected function getForm()
{
    $form = LocationForm::create($this->get(\'form.context\'),\'location\');
    $dm = $this->getDm();
    $form->addTerminals($dm->getRepository(\'RalfBundle:Terminal\')->findAll()->toArray());
    return $form;
}


//... some Actions

    public function createAction()
    {
        $this->getLocation();
        $form = $this->getForm();

        $form->bind($this->get(\'request\'),$this->location);

        if ($form->isValid())
        {
            $dm = $this->getDm();
            $dm->persist($this->location);
            $dm->flush();
            return $this->redirect($this->generateUrl(\'Location\'));
        }
        return $this->render(\'RalfBundle:Ralf:location_create.html.twig\',array(\'form\' => $form));
    }
我可以看到,ѭ6cie接受了表单中的输入值,但是
EmbedMany
-Array
terminals
仍然为空。 我怎么了 感谢您的帮助:D 更新: 好的,找到了解决方案。 在
public function addTerminals($dm)
中的
public function addTerminals($dm)
中,它应如下所示:
public function addTerminals($dm)
{
    $this->add(new ChoiceField(\'terminals.0.terminalName\',array(\'choices\' => $dm)));
    $this->add(new DateField(\'terminals.0.since\',array(\'required\' => true,\'type\'=> \'timestamp\')));
    $this->add(new DateField(\'terminals.0.to\',array(\'required\' => false,\'type\' => \'timestamp\')));
}
\'type\' => \'timestamp\'
是必需的,因为
DateField
将创建
DateTime
-Object,但是文档期望
Int
表示
timestamp
terminals
数组中的一个字段可以用普通的点符号访问。     

解决方法

好的,找到了解决方案。 在
public function addTerminals($dm)
中的
public function addTerminals($dm)
中,它应如下所示:
public function addTerminals($dm)
{
    $this->add(new ChoiceField(\'terminals.0.terminalName\',array(\'choices\' => $dm)));
    $this->add(new DateField(\'terminals.0.since\',array(\'required\' => true,\'type\'=> \'timestamp\')));
    $this->add(new DateField(\'terminals.0.to\',array(\'required\' => false,\'type\' => \'timestamp\')));
}
\'type\' => \'timestamp\'
是必需的,因为
DateField
将创建
DateTime
-Object,但是文档期望
Int
表示
timestamp
terminals
数组中的一个字段可以用普通的点符号访问。     ,实际上,Symfony 2为您提供了一种处理表单中嵌入文档的工具:它称为\“ collection field type \”,它使您可以将其他表单类型(来自其他嵌入文档)嵌入父表单。 可以将其配置为允许/禁止添加/删除嵌入式文档,并且实际上非常强大。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。