Example #1
0
 public function getMiddleCollection(Entity $middleEntity, $foreignKey, $primaryValue)
 {
     if (!$primaryValue) {
         return new Collection();
     }
     return (new GetRecords($middleEntity->where($foreignKey, $primaryValue)->where($this->morph, get_class($this->getLeftEntity()))))->executeAll();
 }
Example #2
0
 public function applyOnEntity(Entity $entity)
 {
     $groups = $this->getAppliedGroups();
     foreach ($groups as $group) {
         if (($group['type'] ?? null) == 'db') {
             $entity->groupBy($group['field']);
         }
     }
 }
Example #3
0
 /**
  * @param       $table
  * @param array $data
  *
  * @return bool|null
  * @throws Exception
  */
 public function updateOrInsert($table, array $data = [])
 {
     $primaryKeys = $this->repository->getCache()->getTablePrimaryKeys($table);
     /**
      * @T00D00 - bug for translations, permissions ...!
      */
     if (!$primaryKeys) {
         return;
     }
     foreach ($primaryKeys as $primaryKey) {
         if (!isset($data[$primaryKey])) {
             return;
         }
     }
     $this->entity->setTable($table);
     foreach ($primaryKeys as $primaryKey) {
         $this->entity->where($primaryKey, $data[$primaryKey]);
     }
     $this->entity->getQuery()->select(['`' . $table . '`.*']);
     $record = $this->entity->one();
     if ($record) {
         return $this->update($table, $data);
     }
     return (new InsertRecord($this->record, $this->entity, $this->repository))->setTables($table)->execute();
 }
Example #4
0
 public function getRecords()
 {
     if (!$this->records) {
         $this->records = $this->entity->all();
     }
     return $this->records;
 }
Example #5
0
 public function execute()
 {
     $entity = new Entity();
     $entity->setRepository(context()->get($this->migration->getRepository()));
     $cache = $entity->getRepository()->getCache();
     foreach ($this->migration->getTables() as $table) {
         $this->sql = [];
         if ($cache->hasTable($table->getName())) {
             $this->updateTable($cache, $table);
         } else {
             $this->installTable($cache, $table);
         }
     }
     if ($this->sqls) {
         $this->applyMigration();
     }
 }
Example #6
0
 /**
  *
  */
 public function execute()
 {
     $data = $this->entity->tabelizeRecord($this->record);
     /**
      * We need to get entity table extensions.
      * Lets hardcode them for now.
      */
     $extensions = ['i18n', 'p17n', ''];
     $table = $this->entity->getTable();
     $primaryKeys = $this->entity->getRepository()->getCache()->getTablePrimaryKeys($table);
     if (!$primaryKeys) {
         throw new Exception('Will NOT delete from table without primary keys ...');
     }
     foreach ($extensions as $ext) {
         if ($ext) {
             $ext = '_' . $ext;
         }
         if ($this->entity->getRepository()->getCache()->hasTable($table . $ext)) {
             /**
              * We will delete record from $table ...
              */
             $query = (new Delete())->setTable($table . $ext);
             /**
              * ... add primary key condition ...
              */
             foreach ($primaryKeys as $key) {
                 $query->where($key, $data[$table][$key]);
             }
             /**
              * ... prepare query ...
              */
             $prepare = $this->repository->prepareQuery($query);
             /**
              *  ... and execute it.
              */
             $this->repository->executePrepared($prepare);
         }
     }
     $this->record->setSaved(false);
     $this->record->setDeleted(true);
     return true;
 }
Example #7
0
 public function getRightRecord(Entity $rightEntity, $foreignKey, $primaryValue)
 {
     if (!$primaryValue) {
         return null;
     }
     $rightEntity->setRepository($this->getLeftEntity()->getRepository());
     $entity = $rightEntity->where($foreignKey, $primaryValue);
     /**
      * Add conditions applied on query.
      */
     foreach ($this->getQuery()->getWhere()->getChildren() as $condition) {
         $entity->where(function (Parenthesis $parenthesis) use($condition) {
             $parenthesis->push($condition);
         });
     }
     foreach ($this->getQuery()->getBinds('where') as $bind) {
         $entity->getQuery()->bind($bind, 'where');
     }
     return (new GetRecords($entity))->executeOne();
 }
Example #8
0
 public function applyOnEntity(Entity $entity)
 {
     $filters = $this->getAppliedFilters();
     $signMapper = ['equals' => '=', 'greater' => '>', 'greaterOrEquals' => '>=', 'lower' => '<', 'lowerOrEquals' => '<=', 'not' => '!=', 'in' => 'IN', 'notIn' => 'NOT IN', 'like' => 'LIKE'];
     foreach ($filters as $filter) {
         if (!is_array($filter['value']) && in_array($filter['options']['method'], ['in', 'notIn'])) {
             $filter['value'] = explode(',', $filter['value']);
         }
         $entity->where($filter['field'], $filter['value'], $signMapper[$filter['options']['method']]);
     }
     $signMapper = ['equals' => '=', 'in' => 'IN', 'notIn' => 'NOT IN', 'not' => '!='];
     $relationFilters = $this->getAppliedRelationFilters();
     foreach ($relationFilters as $relationFilter) {
         $relation = (new Relations())->where('id', $relationFilter['id'])->one();
         if ($relation->dynamic_relation_type_id == 1) {
             $entity->where($relation->onField->field, $relationFilter['value'], $signMapper[$relationFilter['options']['method']]);
         } else {
             if ($relation->dynamic_relation_type_id == 2) {
                 $entity->join('INNER JOIN ' . $relation->showTable->table, $relation->onTable->table . '.id = ' . $relation->showTable->table . '.' . $relation->onField->field, $relation->showTable->table . '.' . $relationFilter['field'] . ' ' . $signMapper[$relationFilter['options']['method']] . ' ' . $entity->getRepository()->getConnection()->quote($relationFilter['value']));
             }
         }
     }
 }
Example #9
0
 /**
  * @return null
  * @throws Exception
  */
 public function execute()
 {
     $data = $this->entity->tabelizeRecord($this->record);
     foreach ($data as $table => $insert) {
         if ($this->tables && !in_array($table, $this->tables)) {
             continue;
         }
         if ($this->record->{$this->entity->getPrimaryKey()}) {
             /**
              * Primary key is already set, we need to update it.
              */
             $insert[$this->entity->getPrimaryKey()] = $this->record->{$this->entity->getPrimaryKey()};
             $this->insert($table, $insert);
         } else {
             /**
              * Primary key is not set yet, we need to set it now.
              */
             $this->record->{$this->entity->getPrimaryKey()} = $this->insert($table, $insert);
             $this->record->setSaved();
         }
     }
     return $this->record->{$this->entity->getPrimaryKey()};
 }
Example #10
0
 public function getMiddleCollection(Entity $middleEntity, $foreignKey, $primaryValue)
 {
     return (new GetRecords($middleEntity->where($foreignKey, $primaryValue)))->executeAll();
 }
Example #11
0
 protected function applyGuessedSorts(Entity $entity)
 {
     if (!$entity->getQuery()->getOrderBy()) {
         $cache = $entity->getRepository()->getCache();
         if ($cache->tableHasField($entity->getTable(), 'order')) {
             $entity->orderBy('`' . $entity->getTable() . '`.`order` ASC');
         } else {
             if ($cache->tableHasField($entity->getTable(), 'ord')) {
                 $entity->orderBy('`' . $entity->getTable() . '`.`ord` ASC');
             } else {
                 if ($cache->tableHasField($entity->getTable(), 'position')) {
                     $entity->orderBy('`' . $entity->getTable() . '`.`position` ASC');
                 } else {
                     if ($cache->tableHasField($entity->getTable(), 'dt_published')) {
                         $entity->orderBy('IF(`' . $entity->getTable() . '`.`dt_published` BETWEEN \'0000-00-00 00:00:01\' AND NOW() , 1, 0) DESC, id ASC');
                     } else {
                         $entity->orderBy('`' . $entity->getTable() . '`.`id` DESC');
                     }
                 }
             }
         }
     }
 }
Example #12
0
 /**
  * @param Entity     $entity
  * @param Repository $repository
  */
 public function __construct(Entity $entity, Repository $repository = null)
 {
     $this->entity = $entity;
     $this->repository = $repository ?: $this->entity->getRepository();
 }
Example #13
0
 public function primaryWhere(Entity $entity, $data, $table)
 {
     $primaryKeys = $entity->getRepository()->getCache()->getTablePrimaryKeys($table);
     if (!$primaryKeys) {
         throw new Exception('Primary key must be set on deletion!');
     }
     foreach ($primaryKeys as $primaryKey) {
         $this->where('`' . $primaryKey . '`', $data[$primaryKey]);
     }
 }
Example #14
0
 public function applyOnEntity(Entity $entity)
 {
     $entity->count()->limit(50)->page($this->get->get('page') ?? 1);
     return $this;
 }
Example #15
0
 public function getData()
 {
     return $this->data ?? $this->entity->all()->toArray();
 }