ManyToMany协会。如何将所有者实体添加到反向实体?

时间:2016-12-04 23:02:59

标签: symfony doctrine-orm

我有2个实体和很多关联:

/**
 * @ORM\Entity
 * @ORM\Table(name="m2m_table1")
 */
class Table1
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false, options={"unsigned":true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Table2", inversedBy="table1", fetch="LAZY", cascade="all")
     * @ORM\JoinTable(name="m2m_links",
     *      joinColumns={@ORM\JoinColumn(name="table1_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="table2_id", referencedColumnName="id")}
     *      )
     */
    private $table2;
    ...
}

/**
 * @ORM\Entity
 * @ORM\Table(name="m2m_table2")
 */
class Table2
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false, options={"unsigned":true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity="Table1", mappedBy="table2", fetch="LAZY", cascade="all")
     */
    private $table1;
    ...
}

我希望有机会向所有者实体添加逆实体,反之亦然。我可以向所有者实体添加逆实体,但我不能将所有者实体添加到逆实体。

$table1 = $em->find('XxxM2mBundle:Table1', 1);
$table2 = $em->find('XxxM2mBundle:Table2', 1);
$table2->addTable1($table1);
$em->flush($table2);

未添加链接。示例被简化,实际上有2种形式,1-st用于调整Table1的链接,2-nd用于调整Table2的链接。我使用Symfony\Bridge\Doctrine\Form\Type\EntityType。第二种形式不适用于此配置。

表格类:

namespace Xxx\M2mBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type as FormType;
use Symfony\Bridge\Doctrine\Form\Type as DoctrineFormType;

class Table2 extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setMethod('post')
            ->add('name', FormType\TextType::class, [
                'required' => true,
                'label' => 'Name',
            ])
            ->add('table1', DoctrineFormType\EntityType::class, [
                'required' => false,
                'expanded' => true,
                'multiple' => true,
                'class' => 'Xxx\\M2mBundle\\Entity\\Table1',
                'choice_label' => 'name',
                'label' => 'Table1',
            ])
            ->add('save', FormType\SubmitType::class, [
                'label' => 'Save',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'Xxx\\M2mBundle\\Entity\\Table2',
        ]);
    }
}

我已将Table2的关联更改为:

/**
 * @ORM\ManyToMany(targetEntity="Table1", inversedBy="table2", fetch="LAZY", cascade="all")
 * @ORM\JoinTable(name="m2m_links",
 *      joinColumns={@ORM\JoinColumn(name="table2_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="table1_id", referencedColumnName="id")}
 *      )
 */
private $table1;

它有所帮助,但我认为这不是一个好的决定,现在我在尝试更新架构The table with name 'm2m_links' already exists.时收到错误./bin/console doctrine:schema:update --dump-sql --force

0 个答案:

没有答案