2013-03-16 24 views
1

我有一个实体,名为News,其中包含与User实体的ManyToMany关系。这意味着一篇文章可以由多位作者撰写。新闻与用户之间的多对多关系

/** 
* @ORM\ManyToMany(targetEntity="GW2\UserBundle\Entity\User") 
*/ 
private $authors; 

的问题是,我想在我使用添加News形式动态地添加作家,但我不知道数据,并如何店保持我的两个之间的关系实体。作者人数不固定,不受限制。

我做了一些关于collection类型的研究,并将其添加到我的NewsType,但我真的不知道如何使用它。


编辑:这是我目前NewsType(未经作者collection):

<?php 
namespace GW2\SiteBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class NewsType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title', 'text', array('error_bubbling' => true)) 
      ->add('introduction', 'textarea', array('attr' => array('class' => 'input-block-level redactor_content'), 'error_bubbling' => true)) 
      ->add('content', 'textarea', array('attr' => array('class' => 'input-block-level redactor_content'), 'error_bubbling' => true)) 
      ->add('publication', 'checkbox', array('required' => false, 'error_bubbling' => true)) 
      ->add('categories', 'entity', array(
       'class' => 'GW2SiteBundle:Category', 
       'property' => 'name', 
       'required' => false, 
       'multiple' => true 
      )) 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'GW2\SiteBundle\Entity\News' 
     )); 
    } 

    public function getName() 
    { 
     return 'gw2_sitebundle_newstype'; 
    } 
} 
+0

向我们展示您的新闻类型,我们将帮助您从那里出发 – 2013-03-17 00:39:05

+0

@ThomasPotaire我用我的NewsType编辑过。 – Yoone 2013-03-17 01:47:49

+0

好吧,你知道如何保存类别吗?它应该是相同的想法 – 2013-03-17 03:27:07

回答

0

我终于加入了collection与FOSUB型固定我的问题,同时指定allow_addallow_delete选项。

->add('authors', 'collection', array(
    'type'=> 'fos_user_username', 
    'allow_add' => true, 
    'allow_delete' => true 
)) 

然后我加入adding-removing solution用JavaScript在形式动态地添加字段(感谢Thomas Potaire)。

相关问题