Symfony2很多人没有保存

时间:2012-11-03 23:29:35

标签: php symfony many-to-many

我有一个M2M关系,但是当我创建一个项目时,关系没有保存,我找不到问题所在。 模特:

class Serie
 {    

/**
 * @ORM\ManyToMany(targetEntity="Magazine", mappedBy="series")
 * */
protected $magazines;

/**
 * Constructor
 */
public function __construct()
{
    $this->magazines = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add magazines
 *
 * @param MyList\DBBundle\Entity\Magazine $magazines
 * @return Serie
 */
public function addMagazine(\MyList\DBBundle\Entity\Magazine $magazines)
{
    $this->magazines[] = $magazines;

    return $this;
}

/**
 * Remove magazines
 *
 * @param MyList\DBBundle\Entity\Magazine $magazines
 */
public function removeMagazine(\MyList\DBBundle\Entity\Magazine $magazines)
{
    $this->magazines->removeElement($magazines);
}

/**
 * Get magazines
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getMagazines()
{
    return $this->magazines;
}
}

杂志课程:

class Magazine
{

/**
 * @ORM\ManyToMany(targetEntity="Serie" , inversedBy="magazines")
 * @ORM\JoinTable(name="magazines_series")
 * */
protected $series;

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

/**
 * Add series
 *
 * @param MyList\DBBundle\Entity\Serie $series
 * @return Magazine
 */
public function addSerie(\MyList\DBBundle\Entity\Serie $serie)
{
    $serie->addMagazine($this);
    $this->series[] = $serie;

    return $this;
}

/**
 * Remove series
 *
 * @param MyList\DBBundle\Entity\Serie $series
 */
public function removeSerie(\MyList\DBBundle\Entity\Serie $series)
{
    $this->series->removeElement($series);
}

/**
 * Get series
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getSeries()
{
    return $this->series;
}
}

控制器     class SerieController扩展了Controller     {     公共功能newAction()     {         $ entity = new Serie();         $ form = $ this-> createForm(new SerieType(),$ entity);

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}

/**
 * Creates a new Serie entity.
 *
 * @Route("/create", name="serie_create")
 * @Method("POST")
 * @Template("DBBundle:Serie:new.html.twig")
 */
public function createAction(Request $request)
{
    $entity  = new Serie();
    $form = $this->createForm(new SerieType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('serie_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}
}

表格:

class SerieType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title')
        ->add('start')
        ->add('end')
        ->add('type')
        ->add('status')
        ->add('magazines','entity',array(
                'class' => 'DBBundle:Magazine',
                'multiple' => true,
                'property' => 'name'
                ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MyList\DBBundle\Entity\Serie'
    ));
}

public function getName()
{
    return 'mylist_dbbundle_serietype';
}
}

我知道有一些问题(即Symfony2-Doctrine: ManyToMany relation is not saved to database),但没有一个问题能解决我的问题。

1 个答案:

答案 0 :(得分:2)

这是因为你永远不会persist()该杂志,表格已经将它添加到实体中,但是Doctrine仍然需要管理它。

if ($form->isValid())

中添加此内容
...
$magazines = $entity->getMagazines();
foreach($magazines as $magazine){
    $em->persist($magazine);
}
...
$em->flush();