0

我在 Symfony2/Doctrine 中实现了一个多对多关系,其中关系本身就是一个实体(这是必要的,因为我需要添加额外的列 -请注意,下面没有提到这些额外的列)。

这里有几个问答,建议将关系视为一个实体(例如,这里这里这里这里)。

我已经通过自动生成了所有 getter 和 setter 方法doctrine:generate:entities(这解释了为什么调用它addCompanie而不是addCompany),所以我认为这涵盖了我的 oneToMany/ManyToOne 关系。

到目前为止,创建用户和公司工作正常。但是,一旦我尝试将用户分配给公司,我就会收到以下错误消息:

在关联 Acme\MyBundle\Entity\Company#employees 上找到类型为 Acme\MyBundle\Entity\User 的实体,但期待 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的样子( 和 之间的关系UserCompany但就这种关系包含更多列 - 这些代码片段中未提及 - 我必须将其创建为实体):

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();
4

1 回答 1

0

非常感谢 Bronchas 在这里的回答,我发现了这一点。控制器逻辑可以实现如下:

// 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(); 
于 2013-08-15T10:49:38.443 回答