2015-10-16 120 views
0

我有兩個實體與這個問題有關,我想要一個連接表能夠在每個表中交叉引用值。Symfony2學說ManyToMany關聯映射無效?

這裏有一個解釋:

實體ContainerType.php:

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use AppBundle\Entity\Containers; 

/** 
* ContainerType 
* 
* @ORM\Table(name="container_type") 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks() 
*/ 
class ContainerType 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="type", type="string", length=255) 
    */ 
    private $type; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_added", type="datetime") 
    */ 
    private $dateAdded; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_modified", type="datetime", nullable=true) 
    */ 
    private $dateModified; 

    /** 
    * @ORM\ManyToMany(targetEntity="Containers", inversedBy="type") 
    * @ORM\JoinTable(name="container_type_containers") 
    **/ 
    private $container; 


    public function __construct() 
    { 
     $this->container = new ArrayCollection(); 
    } 

和實體Containers.php:

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use AppBundle\Entity\ContainerType; 

/** 
* Containers 
* 
* @ORM\Table(name="containers") 
* @ORM\Entity 
*/ 
class Containers 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="number", type="string", length=255) 
    */ 
    private $number; 

    /** 
    * @ORM\ManyToMany(targetEntity="Containers", mappedBy="container") 
    */ 
    private $type; 


    public function __construct() 
    { 
     $this->type = new ArrayCollection(); 
    } 

雖然架構更新工作沒有問題,當我做一種學說:架構:驗證我得到了以下故障:

[Mapping] FAIL - The entity-class 'AppBundle\Entity\Containers' mapping is invalid: 
* The association AppBundle\Entity\Containers#type refers to the owning side field AppBundle\Entity\Containers#container which does not exist. 

但$ container字段確實存在於ContainerType中,所以我不明白爲什麼它試圖在容器實體中引用一個名爲container的字段?

任何人都可以對此有所瞭解嗎?

謝謝 邁克爾

回答

0

我剛剛意識到我的錯誤!

我已經使用了錯誤的目標實體在Containers.php文件,我應該用ContainerType作爲目標,而不是我有這就是爲什麼它試圖找到錯誤的表中的字段集裝箱!

1

我覺得這個代碼應該爲你:)

ContainerType.php工作正常

/** 
    * @ORM\ManyToMany(targetEntity="Containers", inversedBy="containersType") 
    * @ORM\JoinTable(name="container_type_containers", 
    *  joinColumns={@ORM\JoinColumn(name="container_id", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="container_type_id", referencedColumnName="id")}) 
    */ 
    protected $containers; 

Containers.php

/** 
    * @ORM\ManyToMany(targetEntity="ContainerType", mappedBy="containers") 
    */ 
    protected $containersType;