如果不需要,Symfony2会禁用manytoone

时间:2015-09-25 13:06:34

标签: symfony doctrine-orm

有没有办法在formbuilder中禁用多对一连接?

实施例: 我想将菜单条目映射到内部路由或外部链接

所以 到另一个实体或像http://www.google.de

这样的字符串

实体:

/**
 * @ORM\ManyToOne(targetEntity="Unite\MenuBundle\Entity\unmenurouting", inversedBy="unmenus")
 * @ORM\JoinColumn(name="unmenurouting_id", referencedColumnName="id")
 **/
private $unmenurouting;

/**
 * @ORM\ManyToOne(targetEntity="Unite\MenuBundle\Entity\unmenu", inversedBy="unsubmenus")
 * @ORM\JoinColumn(name="unparentmenu_id", referencedColumnName="id")
 **/
private $unmenu;

The Formbuilder

->add('unmenu','entity',array(
                'class'    => 'UniteMenuBundle:unmenu' ,
                'property' => 'unname' ,
                'multiple' => false ,
                'query_builder' => function(EntityRepository $er){
                    return $er->createQueryBuilder('m')
                              ->where('m.unmain = 1')
                              ->andwhere('m.unactive = 1')
                              ;
                }
        ))

现在我收到错误找到关于MyBundle \ MenuBundle \ Entity \ unmenu #unmenurouting

的Doctrine \ Common \ Collections \ ArrayCollection类型的实体

所以我的直接问题是 - 是否可以取消多方连接?

非常感谢

1 个答案:

答案 0 :(得分:0)

解决方案如此简单......

public function setunmenurouting(Unite\MenuBundle\Entity\unmenurouting $unmenurouting = NULL)
{
    if($unmenurouting !== NULL)
        $this->unmenurouting = $unmenurouting;
    $this->unmenurouting = NULL;
}