首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >原则2多重mappedBy?

原则2多重mappedBy?
EN

Stack Overflow用户
提问于 2015-07-08 06:20:27
回答 2查看 6K关注 0票数 6

我在正确设置原则映射时遇到了问题。

我有一个CashRegister实体,它有一个bin位置和一个返回bin位置。两个位置都来自同一类型(BinLocation实体)。

来自CashRegisterCashRegister->getBinLocations()CashRegister->getReturnBinLocations()的传出运行良好,但是如何实现BinLocation->getCashRegisters()返回引用的所有CashRegister实体(binLocation + returnBinLocation)?

代码语言:javascript
复制
/**
 * 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;
    }

    ...

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-08 07:18:16

简单的答案是你不能。mappedBy只接受一个参数。

然而,实现您想要的目标的解决方案是简单的。在BinLocation中创建名为:cashRegisters2的第二个属性,如下所示:

代码语言:javascript
复制
/**
 * @var CashRegister[]
 *
 * @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation")
 */
private $cashRegisters;

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

然后合并getCashRegisters方法中的集合。

代码语言:javascript
复制
/**
 * @return CashRegister[]
 */
public function getCashRegisters()
{
    return new ArrayCollection(
                      array_merge($cashRegisters->toArray(), $cashRegisters2->toArray())
    );
}

还相应地更改您的CashRegister映射:

代码语言:javascript
复制
/**
 * @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;

注意:我没有测试代码。此示例仅用于服务器指南。

Note2: ArrayCollection合并灵感来自于这里:https://stackoverflow.com/a/16871539/2853903

票数 5
EN

Stack Overflow用户

发布于 2018-07-06 13:27:17

我还搜索了解决方案,并为Doctrine做了补丁,这样您就可以将custom_attributes链接到各种实体类型。

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

代码语言:javascript
复制
/**
 * @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;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31284911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档