【问题标题】:Symfony2 - Error with a form that create an association (Many to Many)Symfony2 - 创建关联的表单出错(多对多)
【发布时间】:2012-10-09 19:40:02
【问题描述】:

我在多对多关联中有 2 个实体。

/**
 * @ORM\Table()
 * @ORM\Entity()
 */
class Clown
{
    ...
    /**
     * @ORM\ManyToMany(targetEntity="Appuntamento", inversedBy="partecipanti")
     * @ORM\JoinTable(name="partecipa")
     */
    private $appuntamenti;

    public function __construct() {
        $this->appuntamenti = new ArrayCollection();
    }
    ...
    public function addAppuntamenti(Appuntamento $app)
    {
        $this->appuntamenti[] = $app;
    }    

    public function getAppuntamenti()
    {
        return $this->appuntamenti;
    }
}

/**
 * @ORM\Table()
 * @ORM\Entity()
 */
class Appuntamento
{
    /**
     * @ORM\ManyToMany(targetEntity="Clown", mappedBy="appuntamenti")
     */
    private $partecipanti;

    public function __construct() {
        $this->partecipanti = new ArrayCollection();
    }
    ...
    public function addPartecipanti(Clown $partecipante)
    {
        $partecipante->addAppuntamenti($this);
        $this->partecipanti[] = $partecipante;
    }    

    public function getPartecipanti()
    {
        return $this->partecipanti;
    }
}

然后我创建一个表单,允许设置 Appuntamento 的参与者(又名“partecipanti”)。

namespace Clown\DiaryBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class PartecipantiType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('title')  
            ->add('partecipanti', 'entity', array(
                    'class' => 'ClownDiaryBundle:Clown',
                    'property' => 'drname',
                    'multiple'  => true,
                    'expanded'  => true,
            ))
        ;
    }

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

现在我在控制器中创建动作,一个显示表单,一个更新实体。

/**
 * @Route("/{id}/partecipanti", name="admin_appuntamento_partecipanti")
 * @Template()
 */
public function partecipantiAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('ClownDiaryBundle:Appuntamento')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Appuntamento entity.');
    }

    $partecipantiForm = $this->createForm(new PartecipantiType(), $entity);

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

/**
 * @Route("/{id}/update_partecipanti", name="admin_appuntamento_update_partecipanti")
 * @Method("post")
 * @Template("ClownDiaryBundle:Appuntamento:partecipanti.html.twig")
 */
public function updatePartecipantiAction($id)
{
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('ClownDiaryBundle:Appuntamento')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Appuntamento entity.');
    }

    $partecipantiForm = $this->createForm(new PartecipantiType(), $entity);
    $request = $this->getRequest();
    $partecipantiForm->bindRequest($request);

    if ($partecipantiForm->isValid()) {
        $em->persist($entity);
        $em->flush();

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

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

但是当我在第二个动作中获得帖子时,我尝试持久化对象,没有创建关联(在小丑和 Appuntamento 之间)。 我是不是忘记了什么?

【问题讨论】:

    标签: forms symfony many-to-many


    【解决方案1】:

    您应该通过拥有方持久化您的实体(拥有方负责持久化)。在您的情况下,拥有方是小丑,但您正在尝试通过 Appuntamento 坚持 - 这就是它不坚持的原因。

    尝试更改拥有方或通过小丑实体坚持。

    您可能还想了解更多关于拥有/反面概念的信息:

    【讨论】:

    • 它改变了 Owning 和 Inverse 的一面。感谢 Cyprian!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多