Example #1
0
 protected function makeFields(CollectionInterface $collection)
 {
     return $collection->each(function (Field $field) {
         $type = $field->fieldType->slug;
         $options = [];
         if ($type == 'select') {
             $relation = $field->hasOneSelectRelation;
             if (false && $relation) {
                 $options = $relation->showTable()->createEntity()->limit(100)->all()->keyBy($relation->onField->field)->map(function ($record) {
                     return $record->id;
                 })->toArray();
             }
         }
         return ['field' => $field->field, 'label' => $field->title ?? $field->field, 'type' => $type, 'options' => $options];
     })->keyBy('field');
 }
Example #2
0
 public function fillCollection(CollectionInterface $collection)
 {
     message('Collection of ' . get_class($collection->first()) . ' (' . get_class($this->getLeftEntity()) . ')' . ' ' . get_class($this) . ' ' . get_class($this->getRightEntity()));
     /**
      * Prepare relations on left records.
      */
     message('Left collection has ' . $collection->count() . ' record(s), filling ' . $this->fill);
     $collection->each(function (Record $record) {
         $record->setRelation($this->fill, new Collection());
     });
     /**
      * Get records from right entity.
      */
     $rightCollection = $this->getRightCollection($this->getRightEntity(), $this->foreignKey, $collection->map($this->primaryKey)->unique());
     message('Right collection has ' . $rightCollection->count() . ' record(s)');
     /**
      * Key collection for simpler processing.
      */
     $keyedCollection = $collection->keyBy($this->primaryKey);
     /**
      * Set relations on left records.
      */
     $rightCollection->each(function ($rightRecord) use($keyedCollection) {
         if ($keyedCollection->hasKey($rightRecord->{$this->foreignKey})) {
             $keyedCollection[$rightRecord->{$this->foreignKey}]->getRelation($this->fill)->push($rightRecord);
         }
     });
     /**
      * Fill relations.
      */
     $this->fillCollectionWithRelations($collection);
 }
Example #3
0
 public function applyOnCollection(CollectionInterface $collection)
 {
     $newCollection = null;
     foreach ($this->getAppliedGroups() as $group) {
         if ($group['type'] == 'db') {
             continue;
         }
         if (!$newCollection) {
             $newCollection = $collection->groupBy(function ($item) use($group) {
                 return $item->{$group['field']};
             });
         } else {
             $newCollection = $newCollection->each(function ($groupItems) use($group) {
                 return (new Collection($groupItems))->groupBy(function ($item) use($group) {
                     return $item->{$group['field']};
                 });
             }, true);
         }
     }
     return $newCollection ?? $collection;
 }
Example #4
0
 /**
  * Fill collection of records with all of their relations.
  *
  * @param CollectionInterface $collection
  *
  * @return CollectionInterface
  */
 public function fillCollectionWithRelations(CollectionInterface $collection)
 {
     if (!$collection->count()) {
         return $collection;
     }
     foreach ($this->getWith() as $relation) {
         $relation->fillCollection($collection);
     }
     return $collection;
 }
Example #5
0
 protected function makeFields(CollectionInterface $collection)
 {
     return $collection->each(function (Field $field) {
         return ['field' => $field->field, 'label' => $field->title ?? $field->field, 'applied' => in_array($field->field, $this->getAppliedFields())];
     })->keyBy('field');
 }
Example #6
0
 public function fillCollection(CollectionInterface $collection)
 {
     message('Collection of ' . get_class($collection->first()) . ' (' . get_class($this->getLeftEntity()) . ')' . ' ' . get_class($this) . ' ' . get_class($this->getRightEntity()) . ' Over ' . get_class($this->getMiddleEntity()));
     /**
      * Prepare relations on left records.
      */
     message('Left collection has ' . $collection->count() . ' record(s), filling ' . $this->fill);
     $collection->each(function (Record $record) {
         $record->setRelation($this->fill, new Collection());
     });
     /**
      * Get records from middle entity.
      */
     $middleCollection = $this->getMiddleCollection($this->getMiddleEntity(), $this->leftForeignKey, $collection->map($this->leftPrimaryKey)->unique());
     /**
      * Break with execution if collection is empty.
      */
     message('Middle collection has ' . $middleCollection->count() . ' record(s)');
     if (!$middleCollection->count()) {
         $this->fillCollectionWithRelations($collection);
         return;
     }
     /**
      * Get records from right entity.
      */
     $rightCollection = $this->getRightCollection($this->getRightEntity(), $this->rightPrimaryKey, $middleCollection->map($this->rightForeignKey)->unique());
     message('Right collection has ' . $rightCollection->count() . ' record(s)');
     /**
      * Key collections for simpler processing.
      */
     $keyedLeftCollection = $collection->keyBy($this->leftPrimaryKey);
     $keyedRightCollection = $rightCollection->keyBy($this->rightPrimaryKey);
     /**
      * Set middle collection's relations.
      */
     $middleCollection->each(function ($middleRecord) use($keyedLeftCollection, $keyedRightCollection) {
         if ($keyedRightCollection->hasKey($middleRecord->{$this->rightForeignKey})) {
             $rightRecord = clone $keyedRightCollection[$middleRecord->{$this->rightForeignKey}];
             $rightRecord->setRelation('pivot', $middleRecord);
             $keyedLeftCollection[$middleRecord->{$this->leftForeignKey}]->getRelation($this->fill)->push($rightRecord);
         }
     });
     /**
      * Fill relations.
      */
     $this->fillCollectionWithRelations($collection);
 }