我有以下数据模型
Product: id, description, price
ProductExtraField: id, name
ProductExtraFieldData: product_id, extra_field_id, value
我想使用这个数据模型,这样我的应用程序的用户就可以动态地向产品添加额外的属性。假设添加属性“颜色”。当他们这样做时,将在ProductExtraField
表中创建一行,其值如下:6, 'Color'。现在可以将此属性的值添加到那里的产品中。当这样做时,将在ProductExtraFieldData
表中创建一个新行,其值如下:4, 6, 'Green'。现在我想在一个名为Product
. 我使用以下代码:
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** @Entity @Table(name="product") **/
class Product
public function __construct() {
$this->extraFieldData = new ArrayCollection();
$this->extraField = new ArrayCollection();
}
/** @Id @Column(name="id", type="integer") @GeneratedValue **/
private $id;
/** @Column(name="description", type="string") **/
private $description;
/** @Column(name="price", type="float") **/
private $price;
/**
* @OneToMany(targetEntity="ProductExtraFieldData", mappedBy="product", cascade={"persist"})
* @JoinColumn(name="product_id", referencedColumnName="id")
* @var extraFieldData[]
**/
protected $extraFieldData = null;
/**
* @OneToMany(targetEntity="ProductExtraField", mappedBy="??????")
* @var extraField[]
**/
protected $extraField = null;
当然最后几行是不正确的。我不知道如何加载这些数据。我希望每个产品都可以使用额外字段的名称。我认为$extraField
甚至应该是静态的。如何配置 Doctrine 来加载这些值?保留类中的$extraField
值Product
不是必需的。