コード例 #1
0
ファイル: Entity.php プロジェクト: clevis/orm
 /**
  * @return scalar
  * @throws EntityNotPersistedException
  */
 public final function getId()
 {
     $id = $this->getValue('id', false);
     if ($id === NULL) {
         throw new EntityNotPersistedException(EntityHelper::toString($this) . ' is not persisted.');
     }
     return $id;
 }
コード例 #2
0
ファイル: OneToMany.php プロジェクト: clevis/orm
 /**
  * @param IEntity|scalar|array
  * @return IEntity
  */
 public final function remove($entity)
 {
     $entity = $this->createEntity($entity);
     $param = $this->getMetaData()->getChildParam();
     $parent = $this->getParent();
     $hash = spl_object_hash($entity);
     if (!isset($entity->{$param}) or $entity->{$param} !== $parent) {
         if (func_num_args() >= 2 and (($internalParameter = func_get_arg(1)) === 'handled by ManyToOne' or $internalParameter === 'handled by ManyToOne remove')) {
             $parent->markAsChanged($this->getMetaData()->getParentParam());
             unset($this->add[$hash], $this->edit[$hash], $this->del[$hash]);
             if ($internalParameter !== 'handled by ManyToOne remove') {
                 $this->edit[$hash] = $entity;
             }
             $this->get = NULL;
             return $entity;
         }
         throw new InvalidEntityException('Entity ' . EntityHelper::toString($entity) . ' is not asociated with this entity.');
     }
     $parent->markAsChanged($this->getMetaData()->getParentParam());
     unset($this->add[$hash], $this->edit[$hash], $this->del[$hash]);
     try {
         $entity->__set($param, NULL);
         $this->edit[$hash] = $entity;
     } catch (Exception $e) {
         $this->del[$hash] = $entity;
         // todo wtf chovani, kdyz nemuze existovat bez param tak se vymaze
     }
     $this->get = NULL;
     return $entity;
 }
コード例 #3
0
ファイル: Repository.php プロジェクト: clevis/orm
 /**
  * Ulozit entitu {@see IMapper::persist()} a zapoji ji do repository {@see self::attach()}
  * Jen kdyz se zmenila {@see Entity::isChanged()}
  *
  * Ulozi take vsechny relationship, tedy entity ktere tato entity obsahuje v ruznych vazbach.
  *
  * Vola udalosti:
  * @see Events::PERSIST_BEFORE
  * @see Entity::onBeforePersist()
  * @see Events::PERSIST_BEFORE_UPDATE OR Events::PERSIST_BEFORE_INSERT
  * @see Entity::onBeforeUpdate() OR Entity::onBeforeInsert()
  * @see Events::PERSIST
  * @see Entity::onPersist()
  * @see Events::PERSIST_AFTER_UPDATE OR Events::PERSIST_AFTER_INSERT
  * @see Entity::onAfterUpdate() OR Entity::onAfterInsert()
  * @see Events::PERSIST_AFTER
  * @see Entity::onAfterPersist()
  *
  * @param IEntity
  * @param bool Persist all associations?
  * @return IEntity
  */
 public final function persist(IEntity $entity, $all = true)
 {
     $this->attach($entity);
     $hasId = isset($entity->id);
     if ($hasId) {
         $hasId = $entity->id;
         if (!$entity->isChanged()) {
             return $entity;
         }
     }
     $recursionRelationship = array();
     static $recursionRelationshipCallback;
     if (!$recursionRelationshipCallback) {
         $recursionRelationshipCallback = function (IEntity $entity, array &$recursionRelationship, $all, $persist = false) {
             foreach ($recursionRelationship as $k => $tmp) {
                 list($repository, $key, $value) = $tmp;
                 if ($persist) {
                     if ($all or !isset($value->id)) {
                         $repository->persist($value, $all);
                     }
                 }
                 if ($key !== NULL) {
                     $entity->{$key} = $value;
                 }
                 unset($recursionRelationship[$k]);
             }
         };
     }
     try {
         $hash = spl_object_hash($entity);
         static $recurcion = array();
         if (isset($recurcion[$hash]) and $recurcion[$hash] > 1) {
             throw new RecursiveException("There is an infinite recursion during persist in " . EntityHelper::toString($entity), $recurcion[$hash]);
         }
         if (!isset($recurcion[$hash])) {
             $recurcion[$hash] = 0;
         }
         $recurcion[$hash]++;
         $this->events->fireEvent(Events::PERSIST_BEFORE, $entity);
         $this->events->fireEvent($hasId ? Events::PERSIST_BEFORE_UPDATE : Events::PERSIST_BEFORE_INSERT, $entity);
         $relationshipValues = array();
         $fk = $this->getFkForEntity(get_class($entity));
         foreach ($entity->toArray() as $key => $value) {
             if (isset($fk[$key]) and $value instanceof IEntity) {
                 $repository = $this->getModel()->getRepository($fk[$key]);
                 try {
                     if ($all or !isset($value->id)) {
                         $repository->persist($value, $all);
                     }
                 } catch (RecursiveException $re) {
                     if (isset($value->id)) {
                         $recursionRelationship[] = array($repository, NULL, $value);
                     } else {
                         try {
                             $entity->{$key} = NULL;
                         } catch (Exception $ree) {
                             try {
                                 if ($entity->{$key} !== NULL) {
                                     throw $re;
                                 }
                                 // zmanena ze vyvolalo pravdepodobne chybu v provazni 1:1 ale prenastavolo se na null takze lze pokracovat
                             } catch (Exception $ree) {
                                 throw $re;
                             }
                         }
                         $recursionRelationship[] = array($repository, $key, $value);
                     }
                 }
             } else {
                 if ($value instanceof IRelationship) {
                     $relationshipValues[] = $value;
                 }
             }
         }
         if (!$entity->isChanged()) {
             unset($recurcion[$hash]);
             $recursionRelationshipCallback($entity, $recursionRelationship, $all);
             return $entity;
         }
         if ($id = $this->getMapper()->persist($entity)) {
             $args = array('id' => $id);
             $this->events->fireEvent(Events::PERSIST, $entity, $args);
             $this->identityMap->detach($entity);
             if ($hasId) {
                 $this->identityMap->remove($hasId);
             }
             $id = $entity->id;
             $this->identityMap->add($id, $entity);
             $recursionRelationshipCallback($entity, $recursionRelationship, $all, true);
             foreach ($relationshipValues as $relationship) {
                 $relationship->persist($all);
             }
             $this->events->fireEvent($hasId ? Events::PERSIST_AFTER_UPDATE : Events::PERSIST_AFTER_INSERT, $entity);
             $this->events->fireEvent(Events::PERSIST_AFTER, $entity);
             if ($entity->isChanged() and $entity->getRepository(false) === $this) {
                 $this->getMapper()->persist($entity);
                 $this->events->fireEvent(Events::PERSIST, $entity, $args);
             }
             unset($recurcion[$hash]);
             return $entity;
         }
         throw new BadReturnException(array($this->getMapper(), 'persist', 'TRUE', NULL, '; something wrong with mapper'));
     } catch (Exception $e) {
         unset($recurcion[$hash]);
         $recursionRelationshipCallback($entity, $recursionRelationship, $all);
         throw $e;
     }
 }
コード例 #4
0
ファイル: BaseToMany.php プロジェクト: clevis/orm
 /**
  * @param IEntity
  * @return bool
  * @throws BadEntityException
  */
 protected final function handleCheckAndIgnore(IEntity $entity)
 {
     if ($this->ignore($entity)) {
         return true;
     }
     if (!$this->check($entity)) {
         throw new BadEntityException(get_class($this) . '::check() ' . EntityHelper::toString($entity) . ' is not allowed for that relationship.');
     }
     return false;
 }