/** * @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(); }
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(); }
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(); }
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'])); } } } }
public function getMiddleCollection(Entity $middleEntity, $foreignKey, $primaryValue) { return (new GetRecords($middleEntity->where($foreignKey, $primaryValue)))->executeAll(); }