Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Aug 12, 2021
1 parent a0bc287 commit 5f8b9f8
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/Collection/Helpers/DbalAnyAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function aggregate(
): DbalExpressionResult
{
$joinExpression = array_shift($expression->args);

$joinArgs = $expression->args;
$joins = $expression->joins;
$join = array_pop($joins);
Expand All @@ -34,6 +35,8 @@ public function aggregate(
);

$primaryKey = $join->conventions->getStoragePrimaryKey()[0];
$queryBuilder->addGroupBy('%table.%column', $join->alias, $primaryKey);

return new DbalExpressionResult(
['COUNT(%table.%column) > 0', $join->alias, $primaryKey],
$joins,
Expand Down
3 changes: 3 additions & 0 deletions src/Collection/Helpers/DbalNoneAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function aggregate(
): DbalExpressionResult
{
$joinExpression = array_shift($expression->args);

$joinArgs = $expression->args;
$joins = $expression->joins;
$join = array_pop($joins);
Expand All @@ -34,6 +35,8 @@ public function aggregate(
);

$primaryKey = $join->conventions->getStoragePrimaryKey()[0];
$queryBuilder->addGroupBy('%table.%column', $join->alias, $primaryKey);

return new DbalExpressionResult(
['COUNT(%table.%column) = 0', $join->alias, $primaryKey],
$joins,
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Helpers/DbalQueryBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private function processRelationship(
$targetAlias,
"[$currentAlias.$fromColumn] = %table.[$toColumn]",
[],
$currentConventions
$targetConventions
);

return [$targetAlias, $targetConventions, $targetEntityMetadata, $targetMapper];
Expand Down
11 changes: 8 additions & 3 deletions src/Mapper/Dbal/RelationshipMapperManyHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use function implode;
use function json_encode;
use function md5;
use function strpos;


class RelationshipMapperManyHasMany implements IRelationshipMapperManyHasMany
Expand Down Expand Up @@ -154,13 +155,17 @@ private function fetchByTwoPassStrategy(QueryBuilder $builder, array $values): M
);
$builder->select('%column', "$targetTable.$this->primaryKeyTo");
$builder->addSelect('%column', "$targetTable.$this->primaryKeyFrom");
if ($builder->getClause('having')[0] !== null) {
$builder->addGroupBy('%column', "$targetTable.$this->primaryKeyTo");
$builder->addGroupBy('%column', "$targetTable.$this->primaryKeyFrom");
}

if ($builder->hasLimitOffsetClause()) {
$result = $this->processMultiResult($builder, $values, $targetTable);

} else {
$builder->andWhere('%column IN %any', "$targetTable.$this->primaryKeyFrom", $values);
$result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters());
$result = $this->connection->queryByQueryBuilder($builder);
}

$values = [];
Expand Down Expand Up @@ -250,8 +255,8 @@ private function fetchCounts(QueryBuilder $builder, array $values): array
$builder->addSelect('COUNT(DISTINCT %column) AS [count]', "$targetTable.$this->primaryKeyTo");
$builder->orderBy(null);
$builder->andWhere('%column IN %any', "$targetTable.$this->primaryKeyFrom", $values);
$builder->groupBy('%column', "$targetTable.$this->primaryKeyFrom");
$result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters());
$builder->addGroupBy('%column', "$targetTable.$this->primaryKeyFrom");
$result = $this->connection->queryByQueryBuilder($builder);
}

$counts = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Dbal/RelationshipMapperManyHasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function fetch(QueryBuilder $builder, array $values): MultiEntityItera
$conventions = $this->targetMapper->getConventions();
$primaryKey = $conventions->getStoragePrimaryKey()[0];
$builder->andWhere('%table.%column IN %any', $builder->getFromAlias(), $primaryKey, $values);
$result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters());
$result = $this->connection->queryByQueryBuilder($builder);

$entities = [];
while (($data = $result->fetch())) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/Dbal/RelationshipMapperOneHasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function fetchByOnePassStrategy(QueryBuilder $builder, array $values):
$builder = clone $builder;
$builder->andWhere('%column IN %any', "{$builder->getFromAlias()}.{$this->joinStorageKey}", $values);

$result = $this->connection->queryArgs($builder->getQuerySql(), $builder->getQueryParameters());
$result = $this->connection->queryByQueryBuilder($builder);
$entities = [];

$property = $this->metadataRelationship->property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use NextrasTests\Orm\Publisher;
use NextrasTests\Orm\Tag;
use Tester\Assert;
use Tester\Environment;
use function sort;


require_once __DIR__ . '/../../../bootstrap.php';
Expand Down Expand Up @@ -99,14 +100,17 @@ class EntityRelationshipsTest extends DataTestCase

$tags = [];
foreach ($this->orm->authors->findAll() as $author) {
$authorTags = [];
foreach ($author->books as $book) {
foreach ($book->tags as $tag) {
$tags[] = $tag->id;
$authorTags[] = $tag->id;
}
}
sort($authorTags);
$tags[] = $authorTags;
}

Assert::same([2, 3, 1, 2, 3], $tags);
Assert::same([[1, 2, 2, 3], [3]], $tags);
Assert::equal([], array_filter($queries, function ($count): bool {
return $count != 1;
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,29 @@ class RelationshipManyHasManyTest extends DataTestCase
$books = $tag->books->findBy([
'author->id' => 1,
]);
Assert::same(1, $books->count());
Assert::same(1, $books->countStored());

$books = $tag->books->findBy([
'author->tagFollowers->author->id' => 1,
]);
Assert::same(1, $books->count());
Assert::same(1, $books->countStored());
}


// SELECT "books_x_tags"."tag_id", COUNT(DISTINCT "books_x_tags"."book_id") AS "count"
// FROM "books" AS "books"
// LEFT JOIN "public"."authors" AS "author" ON ("books"."author_id" = "author"."id")
// LEFT JOIN "tag_followers" AS "author_tagFollowers" ON ("author"."id" = "author_tagFollowers"."author_id")
// LEFT JOIN "public"."authors" AS "author_tagFollowers_author"
// ON (("author_tagFollowers"."author_id" = "author_tagFollowers_author"."id") AND "author_tagFollowers_author"."id" = 1)
// LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books_x_tags"."book_id" = "books"."id")
// WHERE "books_x_tags"."tag_id" IN (1)
// GROUP BY "books"."id", "author_tagFollowers_author"."tag_id", "books_x_tags"."tag_id"
// HAVING (COUNT("author_tagFollowers_author"."tag_id") > 0)


public function testSymmetricRelationship(): void
{
$tag = $this->orm->tags->getByIdChecked(2);
Expand Down

0 comments on commit 5f8b9f8

Please sign in to comment.