Doctrine orm 我的一对一/多对一关系在理论上有什么问题?
我在Symfony2/Doctrine中实现了多个关系,其中关系本身就是一个实体(这是必要的,因为我需要添加额外的列-注意,下面没有提到这些额外的列 这里有几个Q&a建议将关系作为一个实体(例如,or) 我已经通过Doctrine orm 我的一对一/多对一关系在理论上有什么问题?,doctrine-orm,many-to-many,associations,Doctrine Orm,Many To Many,Associations,我在Symfony2/Doctrine中实现了多个关系,其中关系本身就是一个实体(这是必要的,因为我需要添加额外的列-注意,下面没有提到这些额外的列 这里有几个Q&a建议将关系作为一个实体(例如,or) 我已经通过原则:generate:entities自动生成了所有getter和setter方法(这解释了为什么它被称为addcompanye,而不是addCompany),所以我会认为这涵盖了我的单域/多域关系 到目前为止,创建一个用户和一个公司效果良好。但是,当我尝试将用户分配给公司时,我会收
原则:generate:entities
自动生成了所有getter和setter方法(这解释了为什么它被称为addcompanye
,而不是addCompany
),所以我会认为这涵盖了我的单域/多域关系
到目前为止,创建一个用户和一个公司效果良好。但是,当我尝试将用户分配给公司时,我会收到以下错误消息:
在上找到Acme\MyBundle\entity\User类型的实体
协会Acme\MyBundle\Entity\Company员工,
但应为Acme\MyBundle\Entity\companyhauser
以下是我的条令定义(yml):
#用户
Acme\MyBundle\Entity\User:
类型:实体
领域:
身份证件:
id:对
类型:整数
发电机:
策略:汽车
一家公司:
公司:
目标实体:Acme\MyBundle\Entity\companyhauser
mappedBy:雇员
#公司
Acme\MyBundle\Entity\Company:
类型:实体
领域:
身份证件:
id:对
类型:整数
发电机:
策略:汽车
一家公司:
员工:
目标实体:Acme\MyBundle\Entity\companyhauser
mappedBy:公司
#公司会计
Acme\MyBundle\Entity\CompanyHauser:
类型:实体
领域:
身份证件:
id:对
类型:整数
发电机:
策略:汽车
许多人:
公司:
目标实体:Acme\MyBundle\Entity\Company
提交人:员工
连接列:
公司编号:
referencedColumnName:id
可为空:false
员工:
目标实体:Acme\MyBundle\Entity\User
提交人:公司
连接列:
用户id:
referencedColumnName:id
可为空: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;
}
}
这就是我的实体类公司的样子
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的样子(用户和公司之间的关系,但是只要这个关系包含更多的列-这些代码片段中没有提到-我就必须将它创建为一个实体):
我的控制器逻辑是:
// 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();
我发现它是巨大的。控制器逻辑可实现如下:
// 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();