2011-12-23 74 views
0

我在使用Symfony2和Doctrine并且希望编辑用中间表格表示的两个表之间的关系。该结构是这样的:编辑Symfony2中的多对多关系

实体旅游存储在桌游存储在表标签

我有一个中间表tour_tags存储每行这两项指标

实体标签:tour_id,TAG_ID。

与实体定义如下:

/** 
* @ORM\Table(name="tour") 
* @ORM\Entity 
*/ 
Tour { 
    /** 
    * @ORM\ManyToMany(targetEntity="Tag") 
    * @ORM\JoinTable(name="tour_tags", 
    *  joinColumns={@ORM\JoinColumn(name="tour_id", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")} 
    * ) 
    */ 
    private $tags; 

    ... 
} 

/** 
* @ORM\Table(name="tags") 
* @ORM\Entity 
*/ 
class Tag { 
    ... 
} 

正如你可以看到我没有把任何有关信息的标签实体。 这个模式在获取相关数据,每个游览的标签等方面工作正常。

但我希望用户能够通过Tour表单中的复选框编辑关系信息。我想包含一组复选框,其中一个用于表中的现有标记。 有了这个文档http://symfony.com/doc/current/cookbook/form/form_collections.html有可能从旅游形式编辑标签名称:

/* The tour form */ 
Class TourType { 
    public function buildForm(FormBuilder $builder, array $options) { 
    ... 
    $builder->add('tags', 'collection', array('type' => new TourTagsType())); 
    } 
} 

但不能创建或删除关系(表tour_tags)。

我正在寻找一种方法在表单中嵌入关系(tour_tag),以便在表格中存在tour_id,tag_id行时显示复选框。

+0

你有没有得到这个想通了? – 2012-11-25 16:26:07

回答

0

在你的旅游类中,添加此构造函数:

public function __construct() { 
    $this->tags = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

而在你TourType:

$builder->add('tags', 'entity', array(
       'class' => 'YourBundle:Tag', 
       'expanded' => true, 
       'multiple' => true 
      )) 
+0

这个答案解决了问题吗? – 2012-11-25 16:26:24

+0

还没有尝试过,我做的是创建和独立的窗体来编辑关系。 – Sergi 2012-11-27 07:54:31