Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use custom alias for resource query builder #485

Open
wants to merge 1 commit into
base: 1.10
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Bundle/Doctrine/ORM/ResourceRepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public function remove(ResourceInterface $resource): void
/**
* @return iterable<int, ResourceInterface>
*/
public function createPaginator(array $criteria = [], array $sorting = []): iterable
public function createPaginator(array $criteria = [], array $sorting = [], string $alias = 'o'): iterable
{
$queryBuilder = $this->createQueryBuilder('o');
$queryBuilder = $this->createQueryBuilder($alias);

$this->applyCriteria($queryBuilder, $criteria);
$this->applySorting($queryBuilder, $sorting);
$this->applyCriteria($queryBuilder, $criteria, $alias);
$this->applySorting($queryBuilder, $sorting, $alias);

return $this->getPaginator($queryBuilder);
}
Expand All @@ -71,14 +71,14 @@ protected function getArrayPaginator($objects): Pagerfanta
return new Pagerfanta(new ArrayAdapter($objects));
}

protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = []): void
protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = [], string $alias = 'o'): void
{
foreach ($criteria as $property => $value) {
if (!in_array($property, array_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
continue;
}

$name = $this->getPropertyName($property);
$name = $this->getPropertyName($property, $alias);

if (null === $value) {
$queryBuilder->andWhere($queryBuilder->expr()->isNull($name));
Expand All @@ -94,23 +94,23 @@ protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = [
}
}

protected function applySorting(QueryBuilder $queryBuilder, array $sorting = []): void
protected function applySorting(QueryBuilder $queryBuilder, array $sorting = [], string $alias = 'o'): void
{
foreach ($sorting as $property => $order) {
if (!in_array($property, array_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) {
continue;
}

if (!empty($order)) {
$queryBuilder->addOrderBy($this->getPropertyName($property), $order);
$queryBuilder->addOrderBy($this->getPropertyName($property, $alias), $order);
}
}
}

protected function getPropertyName(string $name): string
protected function getPropertyName(string $name, string $alias = 'o'): string
{
if (false === strpos($name, '.')) {
return 'o' . '.' . $name;
return $alias . '.' . $name;
}

return $name;
Expand Down