ips*_*sum 5 php mapping orm symfony doctrine-orm
我在正确设置教义映射时遇到问题。
我有一个CashRegister实体,该实体具有容器位置和返回容器位置。两个位置都来自相同的类型(BinLocation实体)。
从发出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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
简单的答案是你不能。mappedBy只接受一个参数。
然而实现您想要的目标的解决方案很简单。BinLocation在Called:中创建第二个属性,cashRegisters2如下所示:
/**
* @var CashRegister[]
*
* @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation")
*/
private $cashRegisters;
/**
* @var CashRegister[]
*
* @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation")
*/
private $cashRegisters2;
Run Code Online (Sandbox Code Playgroud)
然后将集合合并到您的getCashRegisters方法中。
/**
* @return CashRegister[]
*/
public function getCashRegisters()
{
return new ArrayCollection(
array_merge($cashRegisters->toArray(), $cashRegisters2->toArray())
);
}
Run Code Online (Sandbox Code Playgroud)
还要相应地更改您的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="cashRegisters2")
* @ORM\JoinColumn(name="return_bin_location_id", referencedColumnName="id")
*/
private $returnBinLocation;
Run Code Online (Sandbox Code Playgroud)
注意:我没有测试代码。此示例仅供服务器指南。
注2: ArrayCollection合并的灵感来自于这里:/sf/answers/1181007761/
| 归档时间: |
|
| 查看次数: |
3508 次 |
| 最近记录: |