教义2的多重映射

ips*_*sum 5 php mapping orm symfony doctrine-orm

我在正确设置教义映射时遇到问题。

我有一个CashRegister实体,该实体具有容器位置和返回容器位置。两个位置都来自相同的类型(BinLocation实体)。

从发出CashRegisterCashRegister->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)

Mar*_*ark 5

简单的答案是你不能。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/

  • 感谢马克的回答,但这个解决方案看起来有点像黑客。经过一段时间的谷歌搜索(在我写这个问题之前),我注意到只有少数人有同样的问题。是否有另一种方法以更(企业)“教条”的方式实施它?(类,表格,一切都可以改变,唯一的要求是我可以将垃圾箱位置和返回垃圾箱位置存储到收银机并正确取回) (2认同)