简体   繁体   English

Doctrine 2多个mappedBy?

[英]Doctrine 2 multiple mappedBy?

I've got a problem setting up the Doctrine mapping correctly. 我在设置正确的Doctrine映射时遇到了问题。

I have a CashRegister Entity which has a bin location and a return bin location. 我有一个CashRegister实体,它有一个bin位置和一个返回bin位置。 Both locations are from the same Type ( BinLocation Entity). 两个位置都来自相同的类型( BinLocation Entity)。

Outgoing from CashRegister , CashRegister->getBinLocations() and CashRegister->getReturnBinLocations() are working fine, but how can I achieve that BinLocation->getCashRegisters() returns all CashRegister Entities that are referenced ( binLocation + returnBinLocation )? 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;
    }

    ...

}

The simple answer is that you cannot. 简单的答案是你做不到。 mappedBy accepts only one argument. mappedBy只接受一个参数。

The solution to achieve what you want is however simple. 然而,实现您想要的解决方案很简单。 Create a second property in BinLocation called: cashRegisters2 as follows: BinLocation创建第二个属性,名为: cashRegisters2 ,如下所示:

/**
 * @var CashRegister[]
 *
 * @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation")
 */
private $cashRegisters;

/**
 * @var CashRegister[]
 *
 * @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation") 
 */
private $cashRegisters2;

Then merge the Collections in your getCashRegisters method. 然后在getCashRegisters方法中合并集合。

/**
 * @return CashRegister[]
 */
public function getCashRegisters()
{
    return new ArrayCollection(
                      array_merge($cashRegisters->toArray(), $cashRegisters2->toArray())
    );
}

Also change your CashRegister mappings accordingly: 同时更改您的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;

Note: I did not test the code. 注意:我没有测试代码。 This example is to server guide only. 此示例仅适用于服务器指南。

Note2: The ArrayCollection merge was inspired from here: https://stackoverflow.com/a/16871539/2853903 注2: ArrayCollection合并的灵感来自于: https//stackoverflow.com/a/16871539/2853903

I have also searched for solutions and made a patch for Doctrine so you can have custom_attributes linked to a variety of entity types. 我还搜索了解决方案并为Doctrine创建了一个补丁,因此您可以将custom_attributes链接到各种实体类型。

Doctrine 2.6: https://github.com/danielbeeke/doctrine2/commit/2d8530176b872cb490c5c88b8c8e17d8d0091388 Doctrine 2.7: https://github.com/danielbeeke/doctrine2/commit/5bde696848ea9fe7035fadc4d46baa4c0d51f3a2 Doctrine 2.6: https//github.com/danielbeeke/doctrine2/commit/2d8530176b872cb490c5c88b8c8e17d8d0091388 Doctrine 2.7: https//github.com/danielbeeke/doctrine2/commit/5bde696848ea9fe7035fadc4d46baa4c0d51f3a2

/**
 * @Entity
 * @Table(name="product")
 * @HasLifecycleCallbacks
 **/
class Product {

  /**
   * One Product has Many Attributes.
   *
   * @OneToMany(
   *   targetEntity="CustomAttribute",
   *   mappedBy="EntityId",
   *   mappedByType={
   *     "field": "EntityType",
   *     "value": "product"
   *   }
   * )
   *
   * @var $CustomAttributes ArrayCollection
   */
  protected $CustomAttributes;
}


/**
 * @Entity
 * @Table(name="custom_attribute")
 * @HasLifecycleCallbacks
 **/
class CustomAttribute_entity {

  /** @Id @Column(type="integer") @GeneratedValue */
  protected $Id;

  /**
   * Many Attributes have One Entity of the EntityType provided.
   * @ManyToOne(targetEntity="Product", inversedBy="CustomAttributes")
   * @JoinColumn(name="EntityId", referencedColumnName="Id")
   */
  protected $EntityId;

  /** @Column(type="string") */
  protected $EntityType;

  /** @Column(type="string") */
  protected $Type;

  /** @Column(type="string") */
  protected $Value;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM