엔티티 Product
- price
및 currency
의 지정된 등록 정보 상태를 저장해야합니다.doctrine2에서 동일한 엔티티에 일대일 및 일대 다 사용하기
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
개체 스키마
prePersist
에
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
및 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
에 대해 다음 스키마를 가지고 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
번들을 참조했습니다.
나는 마지막 부분을 이해하지 못한다. 이전에 코드에서 사용되지 않은 'getEntityIdentifier'의 오류에 대해 언급했습니다. 또한 나는 여기에'spl_object_hash'를 사용할 점을 얻지 못했습니다. –
@dragoste'getEntityIdentifier'는 doctrine에서 사용되는 메소드입니다. https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/UnitOfWork.php#L2898 – Stafox
아, 그래. 그러나 여전히 코드의 어느 부분 (실제로 어떤 행)이 문제를 일으키는 지 알 수는 없습니다. –