0

我想添加具有 manyToMany 关系的同一对象的 2 个实例。

class A {

     /**

 * @ORM\ManyToMany(targetEntity="RelatedEntity", inversedBy="teachers", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="table1",
 *      joinColumns={@ORM\JoinColumn(name="class_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="related_id", referencedColumnName="id")}
 *      )
 */
protected $myFirstEntities;


     /**

 * @ORM\ManyToMany(targetEntity="RelatedEntity", inversedBy="students", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="table2",
 *      joinColumns={@ORM\JoinColumn(name="class_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="related_id", referencedColumnName="id")}
 *      )
 */
protected $mySecondEntities;
}



class RelatedEntity
{
  /**
 *
 * @ORM\ManyToMany(targetEntity="A", mappedBy="myFirstEntities", cascade={"persist"})
 */
protected $teachers;

  /**
 *
 * @ORM\ManyToMany(targetEntity="A", mappedBy="mySecondEntities", cascade={"persist"})
 */
protected $students;

}

当我使用

        php app/console doctrine:generate:entities MyApp

该程序生成一个添加实体的唯一方法,名为:

public function addRelatedEntity(\MyApp\MyBundle\Entity\RelatedEntity $myFirstEntities)
{
    $this->myFirstEntities[] = $myFirstEntities;
}

所以我无法添加 mySecondEntities。我该怎么办。我可以手动添加它,但自动添加它是不可能的?

我的问题是我可以将特定类的 2 个多对多关系用于同一个类吗?我该怎么做?

4

1 回答 1

0

学说:生成:实体应该在您需要生成实体时使用一次,它不保证生成的映射将 100% 正确,http://docs.doctrine-project.org/projects/doctrine-orm/en/ 2.0.x/reference/tools.html#entity-generation

此命令不适合经常使用。它是一个小帮手,并不能很好地支持所有映射边缘情况。使用此命令后,您仍然需要在实体中进行工作。

您可以为同一个类进行多对多映射,只需手动进行即可

public function addFirstEntity(\MyApp\MyBundle\Entity\RelatedEntity $myFirstEntities)
{
    $this->myFirstEntities[] = $myFirstEntities;
} 

public function addSecondEntity(\MyApp\MyBundle\Entity\RelatedEntity $mySecondEntities)
{
    $this->mySecondEntities[] = $mySecondEntities;
} 
于 2012-08-01T07:00:28.957 回答