private function fetchCounts(QueryBuilder $builder, array $values) { $targetStoragePrimaryKey = $this->targetMapper->getStorageReflection()->getStoragePrimaryKey()[0]; $sourceTable = $builder->getFromAlias(); $builder = clone $builder; $builder->addSelect('%column', "{$sourceTable}.{$this->joinStorageKey}"); $builder->orderBy(NULL); if ($builder->hasLimitOffsetClause()) { $sqls = []; $args = []; foreach ($values as $value) { $build = clone $builder; $build->andWhere('%column = %any', "{$sourceTable}.{$this->joinStorageKey}", $value); $sqls[] = "SELECT %any AS %column, COUNT(*) AS [count] FROM (" . $build->getQuerySql() . ') [temp]'; $args[] = $value; $args[] = $this->joinStorageKey; $args = array_merge($args, $build->getQueryParameters()); } $sql = '(' . implode(') UNION ALL (', $sqls) . ')'; $result = $this->connection->queryArgs($sql, $args); } else { $builder->addSelect('COUNT(%column) AS [count]', "{$sourceTable}.{$targetStoragePrimaryKey}"); $builder->andWhere('%column IN %any', "{$sourceTable}.{$this->joinStorageKey}", $values); $builder->groupBy('%column', "{$sourceTable}.{$this->joinStorageKey}"); $result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters()); } $counts = []; foreach ($result as $row) { $counts[$row->{$this->joinStorageKey}] = $row->count; } return $counts; }
public function remove(IEntity $parent, array $remove) { if (!$remove) { return; } $this->mapperOne->beginTransaction(); $list = $this->buildList($parent, $remove); $this->connection->query('DELETE FROM %table WHERE %column[] IN %any', $this->joinTable, array_keys(reset($list)), array_map('array_values', $list)); }