Php 条令2多重映射比?
我在正确设置条令映射时遇到问题 我有一个Php 条令2多重映射比?,php,symfony,orm,doctrine-orm,mapping,Php,Symfony,Orm,Doctrine Orm,Mapping,我在正确设置条令映射时遇到问题 我有一个收银机实体,它有一个bin位置和一个返回bin位置。两个位置来自同一类型(BinLocationEntity) 从cashlister,cashlister->getBinLocations()和cashlister->getReturnBinLocations()发出的操作正常,但如何实现BinLocation->getcashlisters()返回所有被引用的cashlister实体(binLocation+returnBinLocation) /**
收银机
实体,它有一个bin位置和一个返回bin位置。两个位置来自同一类型(BinLocation
Entity)
从cashlister
,cashlister->getBinLocations()
和cashlister->getReturnBinLocations()
发出的操作正常,但如何实现BinLocation->getcashlisters()
返回所有被引用的cashlister
实体(binLocation
+returnBinLocation
)
/**
*收银机
*
*@ORM\Table(name=“收银机”)
*@ORM\Entity
*@ORM\HasLifecycleCallbacks
*/
分级收银机
{
...
/**
*@var BinLocation
*
*@ORM\ManyToOne(targetEntity=“BinLocation”,inversedBy=“cash registers”)
*@ORM\JoinColumn(name=“bin\u location\u id”,referencedColumnName=“id”)
*/
私人地点;
/**
*@var BinLocation
*
*@ORM\ManyToOne(targetEntity=“BinLocation”,inversedBy=“cash registers”)
*@ORM\JoinColumn(name=“return\u bin\u location\u id”,referencedColumnName=“id”)
*/
私人地点;
/**
*@return-BinLocation
*/
公共函数getBinLocation()
{
返回$this->binLocation;
}
/**
*@return-BinLocation
*/
公共函数getReturnBinLocation()
{
return$this->returnBinLocation;
}
...
}
/**
*二进制定位
*
*@ORM\Table(name=“bin\u位置”)
*@ORM\Entity
*@ORM\HasLifecycleCallbacks
*/
类位
{
...
/**
*@var收银机[]
*
*@ORM\OneToMany(targetEntity=“cashrister”,mappedBy=“binLocation”)收银机;
}
...
}
简单的答案是您不能。mappedBy
只接受一个参数
实现所需的解决方案非常简单。在BinLocation
中创建第二个名为:cashRegisters2
的属性,如下所示:
/**
* @var CashRegister[]
*
* @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation")
*/
private $cashRegisters;
/**
* @var CashRegister[]
*
* @ORM\OneToMany(targetEntity="CashRegister", mappedBy="binLocation")
*/
private $cashRegisters2;
然后在getcashristers
方法中合并集合
/**
* @return CashRegister[]
*/
public function getCashRegisters()
{
return new ArrayCollection(
array_merge($cashRegisters->toArray(), $cashRegisters2->toArray())
);
}
也相应地更改您的收银机
映射:
/**
* @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;
注意:我没有测试代码。此示例仅适用于服务器指南
注2:ArrayCollection合并的灵感来源于此:我还搜索了解决方案,并为条令制作了补丁,因此您可以将自定义属性链接到各种实体类型
原则2.6:
原则2.7:
谢谢马克的回答,但这个解决方案看起来有点像黑客。在谷歌搜索了一段时间后(在我写这个问题之前),我注意到只有少数人有同样的问题。是否有其他方法可以以更(进取的)“原则”的方式实现它?(课程、表格、一切都可以更改唯一的要求是我可以将箱子位置和归还箱子位置存储到收银机中,并将它们正确地取回)问题的根源在于,您不能指出多个mappedBy
字段。因此,您需要使用上述示例中提供的其他功能解决问题。如果您想要更改核心功能,可以尝试在Github上请求更改。在这一阶段,您将获得所有解决方案(我会是“黑客”:
/**
* @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;
/**
* @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;
}