persist() public method

See also: IRepository::persist()
public persist ( Nextras\Orm\Entity\IEntity $entity ) : Nextras\Orm\Entity\IEntity
$entity Nextras\Orm\Entity\IEntity
return Nextras\Orm\Entity\IEntity
Example #1
0
 public function persist(IEntity $entity, $recursive = TRUE)
 {
     $this->identityMap->check($entity);
     if (isset($this->isPersisting[spl_object_hash($entity)])) {
         return $entity;
     }
     $this->isPersisting[spl_object_hash($entity)] = TRUE;
     $this->attach($entity);
     $relationships = [];
     if ($recursive) {
         foreach ($entity->toArray(IEntity::TO_ARRAY_LOADED_RELATIONSHIP_AS_IS) as $k => $v) {
             if ($v instanceof IEntity) {
                 $this->model->getRepositoryForEntity($v)->persist($v);
             } elseif ($v instanceof IRelationshipCollection) {
                 $relationships[] = $v;
             }
         }
     }
     if ($entity->isModified()) {
         $id = $this->mapper->persist($entity);
         $entity->fireEvent('onPersist', [$id]);
     }
     foreach ($relationships as $relationship) {
         $relationship->persist($recursive);
     }
     unset($this->isPersisting[spl_object_hash($entity)]);
     return $entity;
 }
Example #2
0
 /** @inheritdoc */
 public function doPersist(IEntity $entity)
 {
     if (!$entity->isModified()) {
         return;
     }
     $isPersisted = $entity->isPersisted();
     $this->doFireEvent($entity, $isPersisted ? 'onBeforeUpdate' : 'onBeforeInsert');
     $isPersisted && $this->identityMap->remove($entity->getPersistedId());
     // id can change in composite key
     $id = $this->mapper->persist($entity);
     $entity->fireEvent('onPersist', [$id]);
     $this->identityMap->add($entity);
     $this->entitiesToFlush[0][] = $entity;
     $this->doFireEvent($entity, $isPersisted ? 'onAfterUpdate' : 'onAfterInsert');
     $this->doFireEvent($entity, 'onAfterPersist');
 }
Example #3
0
 /** @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;
 }