我在Symfony2 / Doctrine中实现了manyToMany关系,其中关系是实体本身(这是必要的,因为我需要添加其他列 - 注意下面没有提到这些附加列)。
这里有几个Q&amp; As建议将关系视为一个实体(例如here,here,here或here)。< / p>
我已经通过doctrine:generate:entities
自动生成了所有getter和setter方法(这解释了为什么它被称为addCompanie
而不是addCompany
),所以我认为这涵盖了我的oneToMany / ManyToOne的关系。
到目前为止,创建用户和公司工作正常。但是一旦我尝试将用户分配给公司,我就会收到以下错误消息:
找到Acme \ MyBundle \ Entity \ User类型的实体 协会Acme \ MyBundle \ Entity \ Company#employees, 但期待Acme \ MyBundle \ Entity \ CompanyHasUser
这些是我的学说定义(yml):
# User
Acme\MyBundle\Entity\User:
type: entity
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
oneToMany:
companies:
targetEntity: Acme\MyBundle\Entity\CompanyHasUser
mappedBy: employees
# Company
Acme\MyBundle\Entity\Company:
type: entity
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
oneToMany:
employees:
targetEntity: Acme\MyBundle\Entity\CompanyHasUser
mappedBy: companies
# CompanyHasUser
Acme\MyBundle\Entity\CompanyHasUser:
type: entity
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
manyToOne:
companies:
targetEntity: Acme\MyBundle\Entity\Company
inversedBy: employees
joinColumns:
company_id:
referencedColumnName: id
nullable: false
employees:
targetEntity: Acme\MyBundle\Entity\User
inversedBy: companies
joinColumns:
user_id:
referencedColumnName: id
nullable: false
这是我的实体课用户的样子
namespace Acme\MyBundle\Entity;
class User
{
private $id;
private $companies;
public function __construct()
{
$this->companies = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addCompany(\Acme\MyBundle\Entity\CompanyHasUser $companies)
{
$this->companies[] = $companies;
return $this;
}
public function removeCompany(\Acme\MyBundle\Entity\CompanyHasUser $companies)
{
$this->companies->removeElement($companies);
}
public function getCompanies()
{
return $this->companies;
}
}
这是我的实体类公司的样子
namespace Acme\MyBundle\Entity;
class Company
{
private $id;
private $employees;
public function __construct($name, $companyAdmin)
{
$this->employees = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addEmployee(\Acme\MyBundle\Entity\CompanyHasUser $employees)
{
$this->employees[] = $employees;
return $this;
}
public function removeEmployee(\Acme\MyBundle\Entity\CompanyHasUser $employees)
{
$this->employees->removeElement($employees);
}
public function getEmployees()
{
return $this->employees;
}
}
这就是我的实体类 CompanyHasUser 的样子(User
和Company
之间的关系,但只要此关系包含更多列 - 这些代码中未提及片段 - 我必须将其创建为实体):
namespace Acme\MyBundle\Entity;
class CompanyHasUser
{
private $companies;
private $employees;
public function setCompanies(\Acme\MyBundle\Entity\Company $companies)
{
$this->companies = $companies;
return $this;
}
public function getCompanies()
{
return $this->companies;
}
public function setEmployees(\Acme\MyBundle\Entity\User $employees)
{
$this->employees = $employees;
return $this;
}
public function getEmployees()
{
return $this->employees;
}
}
我的控制器逻辑是:
// Create new User (employee)
$user = new User();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
// Create new Company (employer)
$company = new Company();
// Create relationship
$company->addEmployee($user);
$em->persist($company);
// Flush
$em->flush();
答案 0 :(得分:0)
我发现了巨大的thanks to Bronchas answer here。控制器逻辑可以按如下方式实现:
// Create new User (employee)
$user = new User();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
// Create new Company (employer)
$company = new Company();
$em->persist($company);
// Employ User at Company
$employment = new CompanyHasUser();
$employment->setEmployees($user);
$employment->setCompanies($company);
$em->persist($employment);
// Flush
$em->flush();