我需要存储实体Product
- price
和currency
的指定属性状态。
所以我有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
的{{1}}时,我在UnitOfWork中收到了错误。
price
它发生在这里。看起来相同的实体Notice: Undefined index: 000000006c4cf70000000000719f69a2
具有不同的spl_object_hash。
PriceRevision
如何解决此问题?
我在这里发现了几个类似的问题,但是没有解决。其中一些引用public function getEntityIdentifier($entity)
{
return $this->entityIdentifiers[spl_object_hash($entity)];
}
捆绑。
答案 0 :(得分:0)
你可以试着把
$priceRevision->setProduct($this);
进入prePersist
和preUpdate
方法?