Author: Vojtěch Kohout
 protected function updateInDatabase(Entity $entity)
 {
     $mapping = $entity->getClosureTableMapping();
     $parentField = $mapping['parent'];
     // zmenilo se parent_id ??
     $values = $entity->getModifiedRowData();
     if (!array_key_exists($parentField, $values)) {
         return parent::updateInDatabase($entity);
     } else {
         try {
             $this->connection->begin();
             $res = parent::updateInDatabase($entity);
             $tableName = $this->getTable() . '_closure';
             $primaryKey = $this->mapper->getPrimaryKey($this->getTable());
             $idField = $this->mapper->getEntityField($this->getTable(), $primaryKey);
             // odstraneni stare struktury
             $this->connection->query('DELETE a FROM %n AS a' . ' JOIN %n AS d ON a.descendant_id = d.descendant_id' . ' LEFT JOIN %n AS x' . ' ON x.ancestor_id = d.ancestor_id AND x.descendant_id = a.ancestor_id' . ' WHERE d.ancestor_id = ? AND x.ancestor_id IS NULL', $tableName, $tableName, $tableName, $entity->{$idField});
             // vlozeni nove struktury
             if ($values[$parentField] !== NULL) {
                 $this->connection->query('INSERT INTO %n (ancestor_id, descendant_id, depth)' . ' SELECT supertree.ancestor_id, subtree.descendant_id,' . ' supertree.depth+subtree.depth+1' . ' FROM %n AS supertree JOIN %n AS subtree' . ' WHERE subtree.ancestor_id = ?' . ' AND supertree.descendant_id = ?', $tableName, $tableName, $tableName, $entity->{$idField}, $values[$parentField]);
             }
             $this->connection->commit();
             return $res;
         } catch (\Exception $e) {
             $this->connection->rollback();
             throw $e;
         }
     }
 }
Example #2
0
 /**
  * @param Entity $entity
  * @return \DibiResult|int
  */
 protected function updateInDatabase(Entity $entity)
 {
     $primaryKey = $this->mapper->getPrimaryKey($this->getTable());
     $idField = $this->mapper->getEntityField($this->getTable(), $primaryKey);
     $values = $entity->getModifiedRowData();
     $this->changeEmptyStringsToNull($values, $entity->getReflection()->getEntityProperties());
     return $this->connection->query('UPDATE %n SET %a WHERE %n = ?', $this->getTable(), $values, $primaryKey, $entity->{$idField});
 }
 /**
  * @param \LeanMapper\Entity $entity
  * @return mixed|void
  */
 protected function updateInDatabase(\LeanMapper\Entity $entity)
 {
     /** @var TranslatableEntity $entity */
     $table = $this->getTable();
     $primaryKey = $this->mapper->getPrimaryKey($table);
     $idField = $this->mapper->getEntityField($table, $primaryKey);
     $values = array_diff_key($entity->getModifiedRowData(), $entity->getTranslatableColumns());
     if (!empty($values)) {
         $this->connection->query('UPDATE %n SET %a WHERE %n = ?', $this->getTable(), $values, $primaryKey, $entity->{$idField});
     }
     $this->insertTranslation($entity->{$idField}, $entity);
 }
Example #4
0
 /**
  * @param Entity $entity
  * @param array $excludedFields
  * @return bool
  */
 public function compare(Entity $entity, array $excludedFields = null)
 {
     if (!$entity instanceof $this) {
         throw new InvalidArgumentException('Argument $entity has wrong instance type.');
     }
     $_this = $this->getRowData();
     $e = $entity->getRowData();
     if (isset($excludedFields)) {
         $excludedFields = array_flip($excludedFields);
         foreach ($excludedFields as $fieldName => $v) {
             if (array_key_exists($fieldName, $_this)) {
                 unset($_this[$fieldName]);
             }
             if (array_key_exists($fieldName, $e)) {
                 unset($e[$fieldName]);
             }
         }
     }
     return md5(json_encode($_this)) === md5(json_encode($e));
 }
Example #5
0
 public function count()
 {
     return $this->entity->findCount($this->field, $this);
 }
Example #6
0
 public function __call($name, array $arguments)
 {
     if (preg_match('#^(' . implode('|', static::$magicMethodsPrefixes) . ')(.+)$#', $name, $matches)) {
         if (count($arguments) !== 1) {
             throw new InvalidMethodCallException(get_called_class() . "::{$name} expects exactly 1 argument. " . count($arguments) . ' given.');
         }
         list($query) = $arguments;
         if (!$query instanceof IQuery) {
             throw new InvalidArgumentException('Argument 1 passed to ' . get_called_class() . "::{$name} must implement interface Inlm\\QueryObject\\IQuery. " . gettype($query) . ' given.');
         }
         list(, $method, $field) = $matches;
         $field = lcfirst($field);
         return $this->{$method}($field, $query);
     } else {
         return parent::__call($name, $arguments);
     }
 }
 public function logEvent(\LeanMapper\Entity $entity, $action)
 {
     $log = new Log();
     $log->entity_name = $entity->getConventionalName();
     $log->entity_identifier = $entity->id;
     $log->user = $this->userRepository->find($this->user->id);
     $log->logged_at = new DateTime();
     $log->action = $action;
     $this->logRepository->persist($log);
 }
Example #8
0
 /**
  * Persists changes in M:N relationships
  *
  * @param Entity $entity
  */
 protected function persistHasManyChanges(Entity $entity)
 {
     $primaryKey = $this->mapper->getPrimaryKey($this->getTable());
     $idField = $this->mapper->getEntityField($this->getTable(), $primaryKey);
     foreach ($entity->getHasManyRowDifferences() as $key => $difference) {
         list($columnReferencingSourceTable, $relationshipTable, $columnReferencingTargetTable) = explode(':', $key);
         $multiInsert = [];
         foreach ($difference as $value => $count) {
             if ($count > 0) {
                 for ($i = 0; $i < $count; $i++) {
                     $multiInsert[] = [$columnReferencingSourceTable => $entity->{$idField}, $columnReferencingTargetTable => $value];
                 }
             } else {
                 $this->connection->query('DELETE FROM %n WHERE %n = ? AND %n = ? %lmt', $relationshipTable, $columnReferencingSourceTable, $entity->{$idField}, $columnReferencingTargetTable, $value, -$count);
             }
         }
         if (!empty($multiInsert)) {
             $this->connection->query('INSERT INTO %n %ex', $relationshipTable, $multiInsert);
         }
     }
 }
Example #9
0
 /**
  * @param Row|null $arg
  * @param EdgesLoader $edgesLoader
  */
 public function __construct($arg = null, EdgesLoader $edgesLoader)
 {
     parent::__construct($arg);
     $this->edgesLoader = $edgesLoader;
 }
Example #10
0
 /**
  * @param \LeanMapper\Entity $entity
  * @param $id
  * @return void
  */
 public function makeAlive(\LeanMapper\Entity $entity, $id)
 {
     \Nette\Utils\Validators::assert($id, 'numericint:1..');
     $entity->makeAlive($this->entityFactory, $this->connection, $this->mapper);
     $entity->attach($id);
 }
Example #11
0
 public function __construct($arg = null, Dummy $dummy)
 {
     parent::__construct($arg);
     $this->dummy = true;
 }
Example #12
0
 /**
  * @param \LeanMapper\Entity $entity
  * @param $id
  * @throws \LeanMapper\Exception\InvalidStateException
  */
 protected function makeEntityAlive(\LeanMapper\Entity $entity, $id)
 {
     $entity->makeAlive($this->entityFactory, $this->connection, $this->mapper);
     $entity->attach($id);
 }