2017-02-10 89 views
0

我有一個使用複合主鍵引用另一個實體的實體。複合主鍵上的外鍵不起作用

我只是在做一個ManyToOne關係。每家公司可以有很多交易。每家公司都是某些證券交易所的一部分,其唯一標識符既是其上市的證券交易所,也是其股票代碼。

,我得到的,當我嘗試更新架構的錯誤是:

Column name ``id`` referenced for relation from Application\Entity\Trade towards Application\Entity\Company does not exist.

我認爲這是試圖默認id的公司。有沒有辦法在一個表上爲主鍵指定多個外鍵?

<?php 
namespace Application\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
* @ORM\Table(name="trade") 
*/ 
class Trade 
{ 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue 
    * @ORM\Column(name="id",type="integer") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    protected $price; 

    /** 
    * @ORM\Column(type="integer") 
    */ 
    protected $size; 

    /** 
    * @ORM\Column(type="datetime") 
    */ 
    protected $dateTime; 

    /** 
    * @ORM\ManyToOne(targetEntity="Application\Entity\Company", inversedBy="trade") 
    */ 
    protected $company; 

    /** 
    * @return mixed 
    */ 
    public function getPrice() 
    { 
     return $this->price; 
    } 

    /** 
    * @param mixed $price 
    */ 
    public function setPrice($price) 
    { 
     $this->price = $price; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getSize() 
    { 
     return $this->size; 
    } 

    /** 
    * @param mixed $size 
    */ 
    public function setSize($size) 
    { 
     $this->size = $size; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getDateTime() 
    { 
     return $this->dateTime; 
    } 

    /** 
    * @param mixed $dateTime 
    */ 
    public function setDateTime($dateTime) 
    { 
     $this->dateTime = $dateTime; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @param mixed $id 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getCompany() 
    { 
     return $this->company; 
    } 

    /** 
    * @param mixed $company 
    */ 
    public function setCompany($company) 
    { 
     $this->company = $company; 
    } 
} 

這裏的實體公司是否有幫助

<?php 
namespace Application\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
* @ORM\Table(name="company") 
*/ 
class Company 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(length=5) 
    */ 
    protected $symbol; 

    /** 
    * @ORM\Id @ORM\ManyToOne(targetEntity="\Application\Entity\Exchange", inversedBy="company") 
    * @ORM\JoinColumn(name="exchangeKey", referencedColumnName="exchangeKey") 
    */ 
    protected $exchange; 

    /** 
    * @return mixed 
    */ 
    public function getSymbol() 
    { 
     return $this->symbol; 
    } 

    /** 
    * @param mixed $symbol 
    */ 
    public function setSymbol($symbol) 
    { 
     $this->symbol = $symbol; 
    } 

    /** 
    * @return mixed 
    */ 
    public function getExchange() 
    { 
     return $this->exchange; 
    } 

    /** 
    * @param mixed $exchange 
    */ 
    public function setExchange($exchange) 
    { 
     $this->exchange = $exchange; 
    } 
} 

回答

0

你應該能夠引用使用@ORM\JoinColumns註解多列。在裏面你可以定義一個或多個@ORM\JoinColumn註解。

例如

/** 
* @ORM\ManyToOne(targetEntity="Application\Entity\Company", inversedBy="trade") 
* @ORM\JoinColumns({ 
*  @ORM\JoinColumn(name="symbol", referencedColumnName="symbol"), 
*  @ORM\JoinColumn(name="exchange", referencedColumnName="exchangeKey") 
* }); 
*/ 
protected $company; 

我要鏈接到的文件,但所有我能找到的是this

@MoinToOne或@OneToOne關係的@JoinColumn註釋數組與一個具有多個標識符的實體關聯。