Symfony主义-使用ManyToMany

时间:2019-06-18 09:20:01

标签: php symfony doctrine relationship fixtures

我将Symfony 4.2与Doctrine一起使用,并且我想创建具有与两个实体的ManyToMany关系的灯具:

  • 组织
  • 项目

但是当我加载夹具时,联接表(organization_project)总是空的。所以,我不知道我的人际关系是否很好。

对于简历:

  • 许多组织有很多项目
  • 许多项目都有很多组织

数据库视图

database view

这是我的Organization实体

class Organization
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    ...

    /** 
     * @ORM\ManyToMany(targetEntity="App\Entity\Project", mappedBy="organizations")
     */
    private $projects;

    /**
     * @return Collection|Project[]
     */
    public function getProjects(): Collection
    {
        return $this->projects;
    }

    public function addProject(Project $project): self
    {
        if (!$this->projects->contains($project)) {
            $this->projects[] = $project;
            $project->addOrganization($this);
        }

        return $this;
    }

    public function removeProject(Project $project): self
    {
        if ($this->projects->contains($project)) {
            $this->projects->removeElement($project);
            $project->removeOrganization($this);
        }

        return $this;
    }
}

这是我的Project实体:

class Project
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    ...

    /** 
     * @ORM\ManyToMany(targetEntity="App\Entity\Organization", inversedBy="projects")
     */
    private $organizations

    /**
     * @return Collection|Organization[]
     */
    public function getOrganizations(): Collection
    {
        return $this->organizations;
    }

    public function addOrganization(Organization $organization): self
    {
        if (!$this->organizations->contains($organization)) {
            $this->organizations[] = $organization;
        }

        return $this;
    }

    public function removeOrganization(Organization $organization): self
    {
        if ($this->organizations->contains($organization)) {
            $this->organizations->removeElement($organization);
        }

        return $this;
    }

}

这是我的Organization灯具:

class OrganizationFixtures extends Fixture implements DependentFixtureInterface
{
    public const ORGANIZATION_REFERENCE = 'organization-';

    public function load(ObjectManager $manager)
    {
        $organization = new Organization();
        $this->addReference(self::ORGANIZATION_REFERENCE.'0', $organization);
        $organization->addProject($this->getReference('project-0'));

        $manager->persist($organization);
        $manager->flush();
    }

     public function getDependencies()
    {
        return array(
            ProjectFixtures::class
        );
    }
} 

这是我的Project灯具:

class ProjectFixtures extends Fixture
{
    public const PROJECT_REFERENCE = 'project-';

    public function load(ObjectManager $manager)
    {
        $project = new Project();
        $this->addReference(self::PROJECT_REFERENCE.'0', $project);

        $manager->persist($project);
        $manager->flush();
    }
} 

谢谢

1 个答案:

答案 0 :(得分:1)

您的Organization不是关系的owning side。它不具备联接表的知识(在ManyToOne中,它与谁拥有外键有关)。另外,您没有在persist上指定cascading,因此不会保留不拥有一方的关联实体。因此,对于您当前的灯具结构,最佳关联方向将是这样。

class Organization
{
...

    /** 
     * @ORM\ManyToMany(targetEntity="App\Entity\Project", inversedBy="organizations", cascade={"persist"})
     * @ORM\JoinTable(name="organization_project") # this one is not necessary
                                                       putting here for reference
     */
    private $projects;
...
]
class Project
{
...
    /** 
     * @ORM\ManyToMany(targetEntity="App\Entity\Organization", mappedBy="projects", cascade={"persist"})
     */
    private $organizations;
...
}