[英]Using one-to-one and one-to-many on the same entity in doctrine2
我需要存儲實體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
,一切正常且符合預期。 創建了新產品,並且具有相同price
和currency
PriceRevision
。
但是,當我嘗試更改Product
的price
,出現了UnitOfWork錯誤。
Notice: Undefined index: 000000006c4cf70000000000719f69a2
它發生在這里。 看起來同一實體PriceRevision
具有不同的spl_object_hash。
public function getEntityIdentifier($entity)
{
return $this->entityIdentifiers[spl_object_hash($entity)];
}
我怎么解決這個問題?
我在這里找到了幾個類似的問題,但是並沒有解決。 其中一些AuditEntity
捆綁包。
你能不能把
$priceRevision->setProduct($this);
進入prePersist
和preUpdate
方法?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.