2015-10-19 35 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示例 –

回答

相關問題