fix distinct vs group_by (#1901)

This commit is contained in:
Kevin Papst
2020-08-20 22:29:28 +02:00
committed by GitHub
parent b9ee811cbf
commit af4101c9fe
4 changed files with 29 additions and 9 deletions

View File

@@ -261,7 +261,6 @@ class ActivityRepository extends EntityRepository
$qb
->select('a')
->distinct()
->from(Activity::class, 'a')
->leftJoin('a.project', 'p')
->leftJoin('p.customer', 'c')
@@ -362,6 +361,13 @@ class ActivityRepository extends EntityRepository
}
}
// this will make sure, that we do not accidentally create results with multiple rows,
// which would result in a wrong LIMIT with paginated results
$qb->addGroupBy('a');
// the second group by is needed to satisfy SQL standard (ONLY_FULL_GROUP_BY)
$qb->addGroupBy($orderBy);
return $qb;
}

View File

@@ -105,7 +105,7 @@ class CustomerRepository extends EntityRepository
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->addSelect('COUNT(a.id) as activityAmount')
->select('COUNT(a.id) as activityAmount')
->from(Activity::class, 'a')
->join(Project::class, 'p', Query\Expr\Join::WITH, 'a.project = p.id')
->andWhere('a.project = p.id')
@@ -118,7 +118,7 @@ class CustomerRepository extends EntityRepository
}
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->addSelect('COUNT(p.id) as projectAmount')
$qb->select('COUNT(p.id) as projectAmount')
->from(Project::class, 'p')
->andWhere('p.customer = :customer')
;
@@ -165,7 +165,7 @@ class CustomerRepository extends EntityRepository
}
/**
* @deprecated since 1.1 - use getQueryBuilderForFormType() istead - will be removed with 2.0
* @deprecated since 1.1 - use getQueryBuilderForFormType() instead - will be removed with 2.0
*/
public function builderForEntityType($customer)
{
@@ -214,12 +214,10 @@ class CustomerRepository extends EntityRepository
$qb
->select('c')
->distinct()
->from(Customer::class, 'c')
;
$orderBy = 'c.' . $query->getOrderBy();
$qb->orderBy($orderBy, $query->getOrder());
$qb->orderBy('c.' . $query->getOrderBy(), $query->getOrder());
if ($query->isShowVisible()) {
$qb->andWhere($qb->expr()->eq('c.visible', ':visible'));
@@ -269,6 +267,10 @@ class CustomerRepository extends EntityRepository
}
}
// this will make sure, that we do not accidentally create results with multiple rows,
// which would result in a wrong LIMIT with paginated results
$qb->addGroupBy('c');
return $qb;
}

View File

@@ -137,7 +137,6 @@ class InvoiceRepository extends EntityRepository
$qb
->select('i')
->distinct()
->from(Invoice::class, 'i')
;
@@ -152,6 +151,13 @@ class InvoiceRepository extends EntityRepository
$this->addPermissionCriteria($qb, $query->getCurrentUser());
// this will make sure, that we do not accidentally create results with multiple rows,
// which would result in a wrong LIMIT with paginated results
$qb->addGroupBy('i');
// the second group by is needed to satisfy SQL standard (ONLY_FULL_GROUP_BY)
$qb->addGroupBy($orderBy);
return $qb;
}

View File

@@ -255,7 +255,6 @@ class ProjectRepository extends EntityRepository
$qb
->select('p')
->distinct()
->from(Project::class, 'p')
->leftJoin('p.customer', 'c')
;
@@ -367,6 +366,13 @@ class ProjectRepository extends EntityRepository
}
}
// this will make sure, that we do not accidentally create results with multiple rows,
// which would result in a wrong LIMIT with paginated results
$qb->addGroupBy('p');
// the second group by is needed to satisfy SQL standard (ONLY_FULL_GROUP_BY)
$qb->addGroupBy($orderBy);
return $qb;
}