Skip to content

Commit

Permalink
Merge pull request #4802 from dlubitz/bugfix/asset-repository-filter
Browse files Browse the repository at this point in the history
BUGFIX: Filter for assets by asset collection by using constraints
  • Loading branch information
dlubitz authored Dec 13, 2023
2 parents 41f62a5 + b3431dc commit 23928e4
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions Neos.Media/Classes/Domain/Repository/AssetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function findBySearchTermOrTags($searchTerm, array $tags = [], AssetColle
$constraints[] = $query->contains('tags', $tag);
}
$query->matching($query->logicalOr($constraints));
$this->addAssetVariantFilterClause($query);
$this->addAssetVariantToQueryConstraints($query);
$this->addAssetCollectionToQueryConstraints($query, $assetCollection);
return $query->execute();
}
Expand All @@ -104,7 +104,7 @@ public function findByTag(Tag $tag, AssetCollection $assetCollection = null): Qu
{
$query = $this->createQuery();
$query->matching($query->contains('tags', $tag));
$this->addAssetVariantFilterClause($query);
$this->addAssetVariantToQueryConstraints($query);
$this->addAssetCollectionToQueryConstraints($query, $assetCollection);
return $query->execute();
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public function countByTag(Tag $tag, AssetCollection $assetCollection = null): i
public function findAll(AssetCollection $assetCollection = null): QueryResultInterface
{
$query = $this->createQuery();
$this->addAssetVariantFilterClause($query);
$this->addAssetVariantToQueryConstraints($query);
$this->addAssetCollectionToQueryConstraints($query, $assetCollection);
return $query->execute();
}
Expand Down Expand Up @@ -188,7 +188,7 @@ public function findUntagged(AssetCollection $assetCollection = null): QueryResu
{
$query = $this->createQuery();
$query->matching($query->isEmpty('tags'));
$this->addAssetVariantFilterClause($query);
$this->addAssetVariantToQueryConstraints($query);
$this->addAssetCollectionToQueryConstraints($query, $assetCollection);
return $query->execute();
}
Expand Down Expand Up @@ -229,7 +229,7 @@ public function countUntagged(AssetCollection $assetCollection = null): int
public function findByAssetCollection(AssetCollection $assetCollection): QueryResultInterface
{
$query = $this->createQuery();
$this->addAssetVariantFilterClause($query);
$this->addAssetVariantToQueryConstraints($query);
$this->addAssetCollectionToQueryConstraints($query, $assetCollection);
return $query->execute();
}
Expand Down Expand Up @@ -268,9 +268,8 @@ protected function addAssetCollectionToQueryConstraints(QueryInterface $query, A
return;
}

$query->getQueryBuilder()->andWhere(
$query->contains('assetCollections', $assetCollection)
);
$constraints = $query->getConstraint();
$query->matching($query->logicalAnd([$constraints, $query->contains('assetCollections', $assetCollection)]));
}

/**
Expand All @@ -279,14 +278,16 @@ protected function addAssetCollectionToQueryConstraints(QueryInterface $query, A
* @param Query $query
* @return void
*/
protected function addAssetVariantFilterClause(Query $query): void
protected function addAssetVariantToQueryConstraints(QueryInterface $query): void
{
$queryBuilder = $query->getQueryBuilder();

$variantsConstraints = [];
$variantClassNames = $this->reflectionService->getAllImplementationClassNamesForInterface(AssetVariantInterface::class);
foreach ($variantClassNames as $variantClassName) {
$queryBuilder->andWhere('e NOT INSTANCE OF ' . $variantClassName);
$variantsConstraints[] = 'e NOT INSTANCE OF ' . $variantClassName;
}

$constraints = $query->getConstraint();
$query->matching($query->logicalAnd([$constraints, $query->logicalAnd($variantsConstraints)]));
}

/**
Expand Down Expand Up @@ -353,7 +354,7 @@ public function findAllIterator(): IterableResult
{
/** @var Query $query */
$query = $this->createQuery();
$this->addAssetVariantFilterClause($query);
$this->addAssetVariantToQueryConstraints($query);

return $query->getQueryBuilder()->getQuery()->iterate();
}
Expand Down

0 comments on commit 23928e4

Please sign in to comment.