【发布时间】:2015-09-25 21:44:14
【问题描述】:
我在正确设置 Doctrine 映射时遇到问题。
我有一个CashRegister 实体,它有一个垃圾箱位置和一个回收箱位置。两个位置都来自同一类型(BinLocation Entity)。
从CashRegister、CashRegister->getBinLocations() 和CashRegister->getReturnBinLocations() 传出工作正常,但我怎样才能实现BinLocation->getCashRegisters() 返回所有CashRegister 引用的实体(binLocation + returnBinLocation)?
/**
* CashRegister
*
* @ORM\Table(name="cash_registers")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class CashRegister
{
...
/**
* @var BinLocation
*
* @ORM\ManyToOne(targetEntity="BinLocation", inversedBy="cashRegisters")
* @ORM\JoinColumn(name="bin_location_id", referencedColumnName="id")
*/
private $binLocation;
/**
* @var BinLocation
*
* @ORM\ManyToOne(targetEntity="BinLocation", inversedBy="cashRegisters")
* @ORM\JoinColumn(name="return_bin_location_id", referencedColumnName="id")
*/
private $returnBinLocation;
/**
* @return BinLocation
*/
public function getBinLocation()
{
return $this->binLocation;
}
/**
* @return BinLocation
*/
public function getReturnBinLocation()
{
return $this->returnBinLocation;
}
...
}
/**
* BinLocation
*
* @ORM\Table(name="bin_locations")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class BinLocation
{
...
/**
* @var CashRegister[]
*
* @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation") <= Here is the problem, in this case mappedBy need to be an array [binLocation, returnBinLocation]
*/
private $cashRegisters;
/**
* @return CashRegister[]
*/
public function getCashRegisters()
{
return $this->cashRegisters;
}
...
}
【问题讨论】:
标签: php symfony orm doctrine-orm mapping