2015-10-19 34 views
0

我可能已经问过这样一个问题,但我不认为这是完全回答的作品。这实在让我烦恼,因为我真的不明白为什么这是一个问题。Symfony2中使用表单生成实体只有当多个设置为true

我有两个实体,一个叫Containers.php,另一个叫ContainerType.php - 用户可以添加一个容器,并从下拉列表中选择一个类型(使用表单生成器中的实体生成)。这工作,并正确保存在连接表中。但是,如果用户稍后尝试编辑它,则会重置下拉列表(即无法找到现有类型),并且如果从编辑表单中选择了其他类型,则只需将其添加到连接表中,而不是更新你会得到两个关联。

这里是集装箱和ContainerType映射:

/** 
* Containers 
* 
* @ORM\Table(name="containers") 
* @ORM\Entity 
*/ 
class Containers 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="number", type="string", length=255) 
    */ 
    private $number; 

    /** 
    * @ORM\ManyToMany(targetEntity="ContainerType") 
    * 
    * @ORM\JoinColumn(name="id", referencedColumnName="container") 
    */ 
    protected $type; 


    public function __construct() 
    { 
     $this->type = new ArrayCollection(); 
    } 

我的getter和setter方法的类型:

/** 
    * Add type 
    * 
    * @param \AppBundle\Entity\ContainerType $type 
    * 
    * @return Containers 
    */ 
    public function addType(ContainerType $type) 
    { 
     $this->type[] = $type; 

     return $this; 
    } 

    /** 
    * Set type 
    * 
    * @param ArrayCollection $type 
    * 
    * @return Containers 
    */ 
    public function setType($type) 
    { 
     $this->type[] = $type; 

     return $this; 
    } 

    /** 
    * Get type 
    * 
    * @return ContainerType 
    */ 
    public function getType() 
    { 
     return $this->type; 
    } 

这里是ContainerType.php映射:

/** 
* ContainerType 
* 
* @ORM\Table(name="container_type") 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks() 
*/ 
class ContainerType 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="type", type="string", length=255) 
    */ 
    private $type; 

    /** 
    * @param \AppBundle\Entity\Containers 
    * 
    * ORM\@ManyToMany(targetEntity="Containers", mappedBy="type") 
    */ 
    protected $container; 

我添加/编辑容器的表单类型如下:

$builder->add('number' , 'text', array('label' => 'Container Number')); 
     $builder->add('type' , 'entity', 
      array(
       'class' => 'AppBundle:ContainerType', 
       'label' => 'Container Type', 
       'choice_label' => 'type', 
       'empty_value' => '-- Please Select --', 
       'multiple' => false 
      )); 
     $builder->add('save', 'submit', array(
      'attr' => array(
       'class' => 'btn btn-material-blue-800 btn-raised', 
       'value' => 'Save' 
      ), 
     )); 

正如你所看到的,multiple设置为FALSE,这是我想要的 - 我只想显示一个下拉选项,因为只有一种类型可以分配给容器。如果任何人都可以用我的映射来帮助我(如果它们不正确,但是当我运行doctrine:schema:验证它看起来它们是好的),那么我将不胜感激。

否则,我不明白为什么它只能/上编辑正确保存当我设置多为true。当然,这不是唯一的方法吗?

在此先感谢 迈克尔

+0

我不认为你想使用这个用例一个多对多的关系,你需要一个双向多对一,一对多的关系。如果你这样做,那么一切都将按预期工作 –

+0

我永远不能得到ManyToOne关系的工作 - 没有可靠的文档,所以我总是使用ManyToMany,无论它是否需要许多。如何在不失败的情况下使用ManyToOne? –

+0

这里有*非常好的文档。只要看看Doctrine文档:http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html和Symfony文档:http://symfony.com /doc/current/book/doctrine.html。我相信,如果你谷歌它,你会发现很多ManyToOne-OneToMany示例 –

回答

相关问题