findById() public method

Returns entities by primary values.
public findById ( mixed[] $primaryValues ) : Nextras\Orm\Collection\ICollection
$primaryValues mixed[]
return Nextras\Orm\Collection\ICollection
コード例 #1
0
 private function fetchByTwoPassStrategy(SqlBuilder $builder, array $values)
 {
     $builder = clone $builder;
     $builder->addSelect(":{$this->joinTable}({$this->primaryKeyTo}).{$this->primaryKeyTo}");
     $builder->addSelect(":{$this->joinTable}({$this->primaryKeyTo}).{$this->primaryKeyFrom}");
     if ($builder->getLimit() || $builder->getLimit() !== 1) {
         $sqls = $args = [];
         foreach ($values as $value) {
             $builderPart = clone $builder;
             $builderPart->addWhere(":{$this->joinTable}({$this->primaryKeyTo}).{$this->primaryKeyFrom}", $value);
             $sqls[] = $builderPart->buildSelectQuery();
             $args = array_merge($args, $builderPart->getParameters());
         }
         $query = '(' . implode(') UNION ALL (', $sqls) . ')';
         $result = $this->context->queryArgs($query, $args);
     } else {
         $builder->addWhere(":{$this->joinTable}({$this->primaryKeyTo}).{$this->primaryKeyFrom}", $values);
         $result = $this->context->queryArgs($builder->buildSelectQuery(), $builder->getParameters());
     }
     $values = [];
     foreach ($result->fetchAll() as $row) {
         $values[$row->{$this->primaryKeyTo}] = NULL;
     }
     if (count($values) === 0) {
         return new EntityIterator([]);
     }
     $entitiesResult = $this->targetRepository->findById(array_keys($values));
     $entitiesResult->getIterator();
     $entities = [];
     foreach ($result->fetchAll() as $row) {
         $entities[$row->{$this->primaryKeyFrom}][] = $this->targetRepository->getById($row->{$this->primaryKeyTo});
     }
     return new EntityIterator($entities);
 }