簡體   English   中英

在doctrine2中對同一實體使用一對一和一對多

[英]Using one-to-one and one-to-many on the same entity in doctrine2

我需要存儲實體Product - pricecurrency指定屬性狀態。

所以我有以下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

在產品prePersistpreUpdate我執行以下操作:

    $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 ,一切正常且符合預期。 創建了新產品,並且具有相同pricecurrency PriceRevision

但是,當我嘗試更改Productprice ,出現了UnitOfWork錯誤。

Notice: Undefined index: 000000006c4cf70000000000719f69a2

它發生在這里。 看起來同一實體PriceRevision具有不同的spl_object_hash。

public function getEntityIdentifier($entity)
{
    return $this->entityIdentifiers[spl_object_hash($entity)];
}

我怎么解決這個問題?

我在這里找到了幾個類似的問題,但是並沒有解決。 其中一些AuditEntity捆綁包。

你能不能把

$priceRevision->setProduct($this);

進入prePersistpreUpdate方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM