Inheritance: extends Nette\Object
Exemple #1
0
 /**
  * @return QueryBuilder
  */
 public function builder()
 {
     $tableName = $this->getTableName();
     $builder = new QueryBuilder($this->connection->getDriver());
     $builder->from("[{$tableName}]", QueryBuilderHelper::getAlias($tableName));
     return $builder;
 }
 private function fetchCounts(QueryBuilder $builder, array $values)
 {
     $sourceTable = $builder->getFromAlias();
     $targetTable = QueryBuilderHelper::getAlias($this->joinTable);
     $builder = clone $builder;
     $builder->leftJoin($sourceTable, '%table', $targetTable, '%column = %column', $this->joinTable, "{$targetTable}.{$this->primaryKeyTo}", "{$sourceTable}." . $this->targetRepository->getMapper()->getStorageReflection()->getStoragePrimaryKey()[0]);
     $builder->addSelect('%column', "{$targetTable}.{$this->primaryKeyFrom}");
     $builder->orderBy(NULL);
     if ($builder->hasLimitOffsetClause()) {
         $sqls = [];
         $args = [];
         foreach ($values as $value) {
             $build = clone $builder;
             $build->andWhere("%column = %any", $this->primaryKeyFrom, $value);
             $sqls[] = "SELECT %any AS %column, COUNT(*) AS [count] FROM (" . $build->getQuerySql() . ') [temp]';
             $args[] = $value;
             $args[] = $this->primaryKeyFrom;
             $args = array_merge($args, $build->getQueryParameters());
         }
         $sql = '(' . implode(') UNION ALL (', $sqls) . ')';
         $result = $this->connection->queryArgs($sql, $args);
     } else {
         $builder->addSelect('COUNT(%column) as count', $this->primaryKeyTo);
         $builder->andWhere('%column IN %any', $this->primaryKeyFrom, $values);
         $builder->groupBy('%column', $this->primaryKeyFrom);
         $result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters());
     }
     $counts = [];
     foreach ($result as $row) {
         $counts[$row->{$this->primaryKeyFrom}] = $row->count;
     }
     return $counts;
 }