2014-02-13 62 views
3

我有兩個實體:User和Company,它們之間的關係是n:m。在我User.php實體我有這樣的代碼:未定義索引:inverseJoinColumns嘗試定義兩個實體之間的ManyToMany關係

/** 
* @ORM\ManyToMany(targetEntity="PL\CompanyBundle\Entity\Company", mappedBy="users", cascade={"all"}) 
*/ 
protected $companies; 

public function __construct() { 
    $this->companies = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

public function setCompanies(\PL\CompanyBundle\Entity\Company $companies) { 
    $this->companies = $companies; 
} 

public function getCompanies() { 
    return $this->companies; 
} 

在我Company.php實體我有這樣的其他代碼:

/** 
* @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="companies") 
*/ 
protected $users; 

public function __construct() { 
    $this->users = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

但我得到這個錯誤:

ContextErrorException: Notice: Undefined index: inverseJoinColumns in /var/www/html/apps/portal_de_logistica/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1041

什麼是錯的在映射?

編輯重構代碼

從@艾哈邁德 - siouani我做了以下修改以下說明:

User.php

/** 
* @ORM\ManyToMany(targetEntity="PL\CompanyBundle\Entity\Company", inversedBy="users") 
* @ORM\JoinTable(name="fos_user_user_has_company", 
*  JoinColumns={@ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id")} 
*) 
*/ 
protected $companies; 

其中fos_user_user_has_company是表添加了n:m關係。

Company.php

/** 
* @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="companies") 
*/ 
protected $users; 

而現在的錯誤是:

AnnotationException: [Creation Error] The annotation @ORM\JoinTable declared on property Application\Sonata\UserBundle\Entity\User::$companies does not have a property named "JoinColumns". Available properties: name, schema, joinColumns, inverseJoinColumns

任何?

回答

3

定義joinTable時,您可能需要指定joinColumnsinverseJoinColumns。對於雙向許多一對多的定義是會是這樣的,

class User 
{ 
    // ... 

    /** 
    * Bidirectional - Many users have Many companies (OWNING SIDE) 
    * 
    * @ManyToMany(targetEntity="Company", inversedBy="users") 
    * @JoinTable(name="users_companies", 
    *  joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id")} 
    *) 
    **/ 
    private $companies; 

    public function __construct() { 
     $this->companies = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    // ... 
} 

當你的公司類應定義爲跟隨,

class Company 
{ 
    // ... 

    /** 
    * Bidirectional (INVERSE SIDE) 
    * 
    * @ManyToMany(targetEntity="User", mappedBy="companies") 
    */ 
    private $users; 
0

除了@Ahmed解決照顧錯別字的,因爲我做了一個,爲此我得到了第二個錯誤。見我的註釋說:

/** 
* @ORM\ManyToMany(targetEntity="PL\CompanyBundle\Entity\Company", inversedBy="users") 
* @ORM\JoinTable(name="fos_user_user_has_company", 
*  JoinColumns={@ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id")} 
*) 
*/ 
protected $companies; 

但正確的是:

/** 
* @ORM\ManyToMany(targetEntity="PL\CompanyBundle\Entity\Company", inversedBy="users") 
* @ORM\JoinTable(name="fos_user_user_has_company", 
*  joinColumns={@ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id")} 
*) 
*/ 
protected $companies; 

差異是在這一行:

joinColumns={@ORM\JoinColumn(name="fos_user_user_id", referencedColumnName="id")}, 
2

我的錯是,多對多的兩側已經有定義mappedBy,但只有一方應該使用mappedBy,另一方應該使用inversedBy(這通常在主實體中定義,控制集合)。

+0

只是爲了增加這一點,我有一面映射m:1和一面m:m,這引起了我的問題。謝謝@Tobias –