[英]Symfony Doctrine - Make fixtures with ManyToMany
I use Symfony 4.2 with Doctrine and I want to create fixtures with ManyToMany relationships with two entities :我将 Symfony 4.2 与 Doctrine 一起使用,并且我想用两个实体创建具有 ManyToMany 关系的装置:
But when I load fixtures the join table (organization_project) is always empty.但是当我加载装置时,连接表(organization_project)总是空的。 So, I don't know if my relationships is good.所以,我不知道我的人际关系是否良好。
For resume :简历:
Database view数据库视图
Here my Organization
entity这是我的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;
}
}
Here my Project
entity :这是我的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;
}
}
Here my Organization
fixture :这是我的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
);
}
}
Here my Project
fixture :这是我的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();
}
}
Thank you,谢谢,
Your Organization
is not owning side of the relationship.您的Organization
不是关系的拥有方。 It does not hold knowledge of joining table (in ManyToOne
it would be about who is holding foreign key).它不知道加入表(在ManyToOne
它是关于谁持有外键)。 Also you don't specify cascading on persist
, so associated entities on not owning side won't be persisted.此外,您没有在persist
上指定级联,因此不会persist
非拥有方的关联实体。 So for your current fixture structure best association direction would be like this.因此,对于您当前的夹具结构,最佳关联方向是这样的。
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;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.