コード例 #1
0
ファイル: HasMany.php プロジェクト: pckg/database
 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);
 }
コード例 #2
0
ファイル: HasAndBelongsTo.php プロジェクト: pckg/database
 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);
 }