예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function resolve(array $entities, QueryBuilder $query)
 {
     $this->initRelation($entities, []);
     $keys = $this->getKeys($entities);
     $mapping = $this->manager->getConnection()->executeQuery("SELECT {$this->keyThroughFrom}, {$this->keyThroughTo} FROM {$this->tableThrough} WHERE {$this->keyThroughFrom} IN (" . implode(", ", $keys) . ")")->fetchAll(\PDO::FETCH_GROUP | \PDO::FETCH_COLUMN);
     $table = $this->tableThrough;
     $to = $this->keyThroughTo;
     $from = $this->keyThroughFrom;
     $targets = $query->whereIn($this->keyTo, function ($query) use($keys, $table, $to, $from) {
         return $query->select($to)->from($table)->whereIn($from, $keys);
     })->get();
     $metadata = $this->metadata;
     $targetMetadata = $this->targetMetadata;
     $to = $this->keyTo;
     $from = $this->keyFrom;
     foreach ($mapping as $id => $targetIds) {
         $entity = current(array_filter($entities, function ($entity) use($metadata, $from, $id) {
             return $metadata->getValue($entity, $from, true) == $id;
         }));
         $metadata->setValue($entity, $this->name, array_filter($targets, function ($target) use($targetMetadata, $to, $targetIds) {
             return in_array($targetMetadata->getValue($target, $to, true), $targetIds);
         }));
     }
     $this->resolveRelations($query, $targets);
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function resolve(array $entities, QueryBuilder $query)
 {
     $this->initRelation($entities);
     if (!($keys = $this->getKeys($entities))) {
         return;
     }
     $targets = $query->whereIn($this->keyTo, $keys)->get();
     $this->map($entities, $targets);
     $this->resolveRelations($query, $targets);
 }
예제 #3
0
 /**
  * {@inheritdoc}
  */
 public function resolve(array $entities, QueryBuilder $query)
 {
     $this->initRelation($entities, []);
     if (!($keys = $this->getKeys($entities))) {
         return;
     }
     if ($this->orderBy) {
         foreach ($this->orderBy as $column => $order) {
             $query->orderBy($column, $order);
         }
     }
     $targets = $query->whereIn($this->keyTo, $keys)->get();
     $this->map($entities, $targets);
     $this->mapBelongsTo($entities);
     $this->resolveRelations($query, $targets);
 }
예제 #4
0
 /**
  * Resolve additional relations
  *
  * @param QueryBuilder $query
  * @param array        $targets
  */
 protected function resolveRelations(QueryBuilder $query, $targets)
 {
     if (!$targets) {
         return;
     }
     foreach ($query->getRelations() as $name => $q) {
         $this->manager->related($targets, $name, $q);
     }
 }