0

In my event I check if start date is changed if is, then I want to insert new row, and cancel update

if ($args->hasChangedField('startAt')) {
    // cancel update
    // insert new entity
}

I have did something like this

public function preFlush(PreFlushEventArgs $args)
{
    $em = $args->getEntityManager();

    /** @var UnitOfWork $unitOfWork */
    $unitOfWork = $em->getUnitOfWork();

    $identityMap = $unitOfWork->getIdentityMap();

    if (0 === count($identityMap)) {
        return;
    }

    foreach ($identityMap as $entities) {
        foreach ($entities as $entity) {
            if ($entity instanceof Settelment) {
                $original = $unitOfWork->getOriginalEntityData($entity);

                if ($entity->getStartAt() !== $original['startAt']) {
                    $em->detach($entity); // remove entity
                }
            }
        }
    }

}

But now I remove entity and its ok but I dont define here new object and persist it, and I got extra row in database (this is what I want) but how does it work ?? why i get extra row if I didnt create entity.

kskaradzinski
  • 4,954
  • 10
  • 48
  • 70
  • Doctrine 2 events have quite a few restrictions with preUpdate being the worse. http://doctrine-orm.readthedocs.org/en/latest/reference/events.html#preupdate . You might have better luck using a listener and maybe the preFlush event. – Cerad Sep 15 '14 at 13:53
  • http://stackoverflow.com/questions/9071094/how-to-re-save-the-entity-as-another-row-in-doctrine-2 A versioning of the above will answer your question – Matthew A Thomas Sep 16 '14 at 15:43

0 Answers0