Exemplo n.º 1
0
 /**
  * Prepare query from entity, fetch records and fill them with relations.
  *
  * @return Collection
  */
 public function executeAll()
 {
     $repository = $this->repository;
     $entity = $this->entity;
     $prepare = $repository->prepareQuery($entity->getQuery(), $entity->getRecordClass());
     if ($execute = $repository->executePrepared($prepare) && ($results = $repository->fetchAllPrepared($prepare))) {
         $collection = new Collection($results);
         if ($entity->getQuery()->isCounted()) {
             $prepareCount = $repository->prepareSQL('SELECT FOUND_ROWS()');
             $repository->executePrepared($prepareCount);
             $collection->setTotal($prepareCount->fetch(PDO::FETCH_COLUMN));
             $entity->count(false);
         }
         $collection->setEntity($entity)->setSaved();
         return $entity->fillCollectionWithRelations($collection);
     }
     return new Collection();
 }
Exemplo n.º 2
0
 protected function install($dynamicTables = [], $relations = [])
 {
     $fieldTypes = (new FieldTypes())->all()->keyBy('slug');
     $tables = new Collection();
     foreach ($dynamicTables as $dynamicTable) {
         $record = (new Table())->setData(['table' => $dynamicTable['table'], 'repository' => $dynamicTable['repository'] ?? null]);
         $record->save();
         $tables->push($record, $dynamicTable['table']);
     }
     $fields = new Collection();
     $tabs = new Collection();
     $actions = new Collection();
     foreach ($dynamicTables as $dynamicTable) {
         $table = $tables->getKey($dynamicTable['table']);
         foreach ($dynamicTable['_fields'] as $i => $field) {
             $field = (new Field())->setData(['field' => $field['field'], 'dynamic_table_id' => $table->id, 'dynamic_field_type_id' => $fieldTypes->getKey($field['type'])->id, 'order' => $i * 10]);
             $field->save();
             $fields->push($field, $table->table . '.' . $field->field);
         }
         foreach ($dynamicTable['_tabs'] as $i => $tab) {
             $record = (new Tab())->setData(['dynamic_table_id' => $table->id, 'order' => $i * 10]);
             $record->save();
             $tabs->push($record, $table->table . '.' . $i);
         }
         foreach ($dynamicTable['_actions'] as $i => $action) {
             $record = (new TableAction())->setData(['dynamic_table_id' => $table->id, 'slug' => $action['slug'], 'type' => $action['type']]);
             $record->save();
             $actions->push($record, $table->table . '.' . $action->slug);
         }
     }
     $tables = (new Tables())->all()->keyBy('table');
     $fields = (new Fields())->withTable()->all()->keyBy(function (Field $field) {
         return $field->table->table . '.' . $field->field;
     });
     $relationTypes = (new RelationTypes())->all()->keyBy('slug');
     foreach ($relations as $relation) {
         $record = (new Relation())->setData(['on_table_id' => $tables->getKey($relation['on_table'])->id, 'on_field_id' => $fields->getKey(($relation['relation_type'] == 'belongs_to' ? $relation['on_table'] : $relation['show_table']) . '.' . $relation['on_field'])->id, 'show_table_id' => $tables->getKey($relation['show_table'])->id, 'dynamic_relation_type_id' => $relationTypes->getKey($relation['relation_type'])->id, 'value' => $relation['value']])->save();
     }
 }