我需要存储实体Product
- price
和currency
的指定属性状态。在doctrine2的同一实体中使用一对一和一对多
所以,我对于Product
以下模式:
Product:
type: entity
table: product
fields:
id:
type: string
length: 36
id: true
generator:
strategy: UUID
price:
type: float
currency:
type: string
length: 3
created:
type: datetime
gedmo:
timestampable:
on: create
updated:
type: datetime
gedmo:
timestampable:
on: update
lifecycleCallbacks:
prePersist: [ prePersist ]
preUpdate: [ preUpdate ]
oneToOne:
lastPriceRevision:
cascade: ["persist", "remove"]
targetEntity: PriceRevision
joinColumn:
name: last_price_revision_id
referencedColumnName: id
oneToMany:
priceRevisions:
cascade: ["persist", "remove"]
targetEntity: PriceRevision
mappedBy: product
而且PriceRevision
实体模式
PriceRevision:
type: entity
table: price_revision
fields:
id:
type: string
length: 36
id: true
generator:
strategy: UUID
price:
type: float
currency:
type: string
length: 3
created:
type: datetime
gedmo:
timestampable:
on: create
updated:
type: datetime
gedmo:
timestampable:
on: update
lifecycleCallbacks: { }
manyToOne:
product:
targetEntity: Product
inversedBy: priceRevisions
joinColumn:
name: product_id
referencedColumnName: id
在产品prePersist
和preUpdate
我做了以下内容:
$priceRevision = $this->getLastPriceRevision();
if (!$priceRevision || $this->getPrice() !== $priceRevision->getPrice()
|| $this->getCurrency() !== $priceRevision->getCurrency()
) {
$priceRevision = new PriceRevision();
$priceRevision->setPrice($this->getPrice());
$priceRevision->setCurrency($this->getCurrency());
$this->addPriceRevision($priceRevision);
$this->setLastPriceRevision($priceRevision);
}
在创建Product
- 所有工作正常,如预期。已创建新产品,它有PriceRevision
与price
和currency
相同。
但是,当我试图改变Product
的price
我得到了UnitOfWork中的错误。
Notice: Undefined index: 000000006c4cf70000000000719f69a2
它发生在这里。看起来像同一个实体PriceRevision
有不同的spl_object_hash。
public function getEntityIdentifier($entity)
{
return $this->entityIdentifiers[spl_object_hash($entity)];
}
我该如何解决这个问题?
我在这里发现了几个类似的问题,但他们没有解决。其中一些参考AuditEntity
包。
我不明白最后一部分。你在前面的代码中没有提到'getEntityIdentifier'中的错误。我也没有真正在这里使用'spl_object_hash'。 –
@dragoste'getEntityIdentifier'是教义中使用的一种方法。 https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/UnitOfWork.php#L2898 – Stafox
啊,好的。但是,我们仍然不知道代码的哪一部分(实际上哪一行)会导致此问题。 –