/** @inheritdoc */ public function persist(IEntity $entity, $recursive = TRUE, &$queue = NULL) { $this->identityMap->check($entity); $entityHash = spl_object_hash($entity); if (isset($queue[$entityHash]) && $queue[$entityHash] === TRUE) { return $entity; } $isRunner = $queue === NULL; if ($isRunner) { $queue = []; } $queue[$entityHash] = TRUE; try { $this->attach($entity); $isPersisted = $entity->isPersisted(); $this->fireEvent($entity, 'onBeforePersist'); $this->fireEvent($entity, $isPersisted ? 'onBeforeUpdate' : 'onBeforeInsert'); $isModified = $entity->isModified(); if ($recursive) { list($prePersist, $postPersist) = PersistanceHelper::getLoadedRelationships($entity); foreach ($prePersist as $value) { $this->model->getRepositoryForEntity($value)->persist($value, $recursive, $queue); } } if ($isModified) { if ($isPersisted) { // id can change (composite key) $this->identityMap->remove($entity->getPersistedId()); } $id = $this->mapper->persist($entity); $entity->fireEvent('onPersist', [$id]); $this->identityMap->add($entity); $this->entitiesToFlush[0][] = $entity; } if ($recursive) { foreach ($postPersist as $postPersistValue) { $hash = spl_object_hash($postPersistValue); if (!isset($queue[$hash])) { $queue[$hash] = $postPersistValue; } } if ($isRunner) { reset($queue); while ($value = current($queue)) { $hash = key($queue); next($queue); if ($value === TRUE) { continue; } if ($value instanceof IEntity) { $this->model->getRepositoryForEntity($value)->persist($value, $recursive, $queue); } elseif ($value instanceof IRelationshipCollection) { $value->persist($recursive, $queue); } $queue[$hash] = TRUE; } } } if ($isModified) { $this->fireEvent($entity, $isPersisted ? 'onAfterUpdate' : 'onAfterInsert'); $this->fireEvent($entity, 'onAfterPersist'); } } catch (\Exception $e) { } // finally workaround if ($isRunner) { $queue = NULL; } if (isset($e)) { throw $e; } return $entity; }
/** @inheritdoc */ public function persist(IEntity $entity, $withCascade = TRUE) { PersistanceHelper::getCascadeQueue($entity, $this, $withCascade, $queue); foreach ($queue as $object) { if ($object instanceof IEntity) { $repository = $this->configuration[2][get_class($object)]; $this->loader->getRepository($repository)->doPersist($object); } elseif ($object instanceof IRelationshipCollection) { $object->doPersist(); } } return $entity; }