在doctrine2

时间:2016-10-05 06:32:43

标签: php symfony doctrine-orm entity-relationship

我需要存储实体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时 - 一切正常并且符合预期。已创建新产品,PriceRevision具有相同的pricecurrency

但当我尝试更改Product的{​​{1}}时,我在UnitOfWork中收到了错误。

price

它发生在这里。看起来相同的实体Notice: Undefined index: 000000006c4cf70000000000719f69a2 具有不同的spl_object_hash。

PriceRevision

如何解决此问题?

我在这里发现了几个类似的问题,但是没有解决。其中一些引用public function getEntityIdentifier($entity) { return $this->entityIdentifiers[spl_object_hash($entity)]; } 捆绑。

1 个答案:

答案 0 :(得分:0)

你可以试着把

$priceRevision->setProduct($this);

进入prePersistpreUpdate方法?