Ejemplo n.º 1
0
 /**
  * Vytvori / nacte / vrati entitu.
  * @param IEntity|scalar|array
  * @param bool
  * @return IEntity|NULL null only if not invasive
  * @throws EntityNotFoundException
  */
 protected final function createEntity($entity, $invasive = true)
 {
     if ($entity instanceof IEntity) {
         $model = $entity->getModel(false);
         if ($model and $this->parent->getModel(false) !== $model) {
             $this->parent->fireEvent('onAttachModel', NULL, $model);
         }
     }
     $repository = $this->getChildRepository($invasive);
     // todo neni mozne kdyz parent neni pripojen na repo
     if (!$repository) {
         return NULL;
     }
     if (!$entity instanceof IEntity and (is_array($entity) or $entity instanceof Traversable)) {
         $array = $entity instanceof Traversable ? iterator_to_array($entity) : $entity;
         $entity = NULL;
         if (isset($array['id'])) {
             $entity = $repository->getById($array['id']);
         }
         if (!$entity) {
             if (!$invasive) {
                 return NULL;
             }
             $entityName = $repository->getEntityClassName($array);
             $entity = new $entityName();
             // todo construct pak nesmy mit povine parametry
             $repository->attach($entity);
         }
         if ($invasive) {
             $entity->setValues($array);
         }
     }
     if (!$entity instanceof IEntity) {
         $id = $entity;
         $entity = $repository->getById($id);
         if (!$entity) {
             if (!$invasive) {
                 return NULL;
             }
             throw new EntityNotFoundException("Entity '{$id}' not found in `" . get_class($repository) . "`");
         }
     }
     if ($invasive) {
         $repository->attach($entity);
     } else {
         if (!$repository->isAttachableEntity($entity)) {
             return NULL;
         }
     }
     return $entity;
 }