refactored repositories and DB queries (#5026)

* removed unused teams from export order
* added new paginator for query instead of querybuilder
* added field hydrate enums
* hide PARTIAL deprecation
* never log deprecations in production
* replaced InvoiceLoader with native Doctrine feature
* prevent excessive permission queries
* support loading customers of team
* improved findByIds
* internalized API
* fix null string deprecations
This commit is contained in:
Kevin Papst
2024-08-27 10:11:19 +02:00
committed by GitHub
parent 9e3d243b4b
commit 9d933f62c0
81 changed files with 1648 additions and 1436 deletions

View File

@@ -16,14 +16,16 @@ use App\Entity\Project;
use App\Entity\Team;
use App\Entity\User;
use App\Repository\Loader\CustomerLoader;
use App\Repository\Paginator\LoaderPaginator;
use App\Repository\Paginator\LoaderQueryPaginator;
use App\Repository\Paginator\PaginatorInterface;
use App\Repository\Query\CustomerFormTypeQuery;
use App\Repository\Query\CustomerQuery;
use App\Repository\Query\CustomerQueryHydrate;
use App\Utils\Pagination;
use Doctrine\DBAL\ParameterType;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
@@ -37,22 +39,28 @@ class CustomerRepository extends EntityRepository
/**
* @param int[] $customerIDs
* @return Customer[]
* @return array<Customer>
*/
public function findByIds(array $customerIDs): array
{
$ids = array_filter(
array_unique($customerIDs),
function ($value) {
return $value > 0;
}
);
if (\count($ids) === 0) {
return [];
}
$qb = $this->createQueryBuilder('c');
$qb
->where($qb->expr()->in('c.id', ':id'))
->setParameter('id', $customerIDs)
->setParameter('id', $ids)
;
$customers = $qb->getQuery()->getResult();
$loader = new CustomerLoader($qb->getEntityManager(), true);
$loader->loadResults($customers);
return $customers;
return $this->getCustomers($this->prepareCustomerQuery($qb->getQuery()), new CustomerQuery());
}
public function saveCustomer(Customer $customer): void
@@ -120,6 +128,8 @@ class CustomerRepository extends EntityRepository
/**
* Returns a query builder that is used for CustomerType and your own 'query_builder' option.
*
* @internal
*/
public function getQueryBuilderForFormType(CustomerFormTypeQuery $query): QueryBuilder
{
@@ -163,18 +173,14 @@ class CustomerRepository extends EntityRepository
private function getQueryBuilderForQuery(CustomerQuery $query): QueryBuilder
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb = $this->createQueryBuilder('c');
$qb
->select('c')
->from(Customer::class, 'c')
;
if (\count($query->getCustomerIds()) > 0) {
$qb->andWhere($qb->expr()->in('c.id', ':id'))->setParameter('id', $query->getCustomerIds());
}
if ($query->getCountry() !== null) {
$qb
->andWhere($qb->expr()->eq('c.country', ':country'))
->setParameter('country', $query->getCountry())
;
$qb->andWhere($qb->expr()->eq('c.country', ':country'))->setParameter('country', $query->getCountry());
}
foreach ($query->getOrderGroups() as $orderBy => $order) {
@@ -190,11 +196,9 @@ class CustomerRepository extends EntityRepository
}
if ($query->isShowVisible()) {
$qb->andWhere($qb->expr()->eq('c.visible', ':visible'));
$qb->setParameter('visible', true, ParameterType::BOOLEAN);
$qb->andWhere($qb->expr()->eq('c.visible', ':visible'))->setParameter('visible', true, ParameterType::BOOLEAN);
} elseif ($query->isShowHidden()) {
$qb->andWhere($qb->expr()->eq('c.visible', ':visible'));
$qb->setParameter('visible', false, ParameterType::BOOLEAN);
$qb->andWhere($qb->expr()->eq('c.visible', ':visible'))->setParameter('visible', false, ParameterType::BOOLEAN);
}
$this->addPermissionCriteria($qb, $query->getCurrentUser(), $query->getTeams());
@@ -227,6 +231,10 @@ class CustomerRepository extends EntityRepository
return new Pagination($this->getPaginatorForQuery($query), $query);
}
/**
* FIXME make this private and remove the widget that this currently uses
* @return int<0, max>
*/
public function countCustomersForQuery(CustomerQuery $query): int
{
$qb = $this->getQueryBuilderForQuery($query);
@@ -237,27 +245,81 @@ class CustomerRepository extends EntityRepository
->select($qb->expr()->countDistinct('c.id'))
;
return (int) $qb->getQuery()->getSingleScalarResult();
return (int) $qb->getQuery()->getSingleScalarResult(); // @phpstan-ignore-line
}
protected function getPaginatorForQuery(CustomerQuery $query): PaginatorInterface
/**
* @return PaginatorInterface<Customer>
*/
private function getPaginatorForQuery(CustomerQuery $customerQuery): PaginatorInterface
{
$counter = $this->countCustomersForQuery($query);
$qb = $this->getQueryBuilderForQuery($query);
$counter = $this->countCustomersForQuery($customerQuery);
$query = $this->createCustomerQuery($customerQuery);
return new LoaderPaginator(new CustomerLoader($qb->getEntityManager()), $qb, $counter);
return new LoaderQueryPaginator(new CustomerLoader($this->getEntityManager(), $customerQuery), $query, $counter);
}
/**
* @return Query<Customer>
*/
private function createCustomerQuery(CustomerQuery $customerQuery): Query
{
$query = $this->getQueryBuilderForQuery($customerQuery)->getQuery();
$query = $this->prepareCustomerQuery($query);
foreach ($customerQuery->getHydrate() as $hydrate) {
switch ($hydrate) {
case CustomerQueryHydrate::TEAMS:
// does not yet work, see https://github.com/doctrine/orm/pull/8391
// $query->setFetchMode(Customer::class, 'teams', ClassMetadata::FETCH_EAGER);
break;
case CustomerQueryHydrate::TEAM_MEMBER:
// does not yet work, see https://github.com/doctrine/orm/issues/11254
// $query->setFetchMode(Customer::class, 'teams', ClassMetadata::FETCH_EAGER);
// $query->setFetchMode(Team::class, 'members', ClassMetadata::FETCH_EAGER);
// $query->setFetchMode(TeamMember::class, 'user', ClassMetadata::FETCH_EAGER);
break;
}
}
return $query;
}
/**
* @param Query<Customer> $query
* @return Query<Customer>
*/
public function prepareCustomerQuery(Query $query): Query
{
$this->getEntityManager()->getConfiguration()->setEagerFetchBatchSize(300);
$query->setFetchMode(Customer::class, 'meta', ClassMetadata::FETCH_EAGER);
return $query;
}
/**
* @return Customer[]
*/
public function getCustomersForQuery(CustomerQuery $query): iterable
public function getCustomersForQuery(CustomerQuery $customerQuery): array
{
// this is using the paginator internally, as it will load all joined entities into the working unit
// do not "optimize" to use the query directly, as it would results in hundreds of additional lazy queries
$paginator = $this->getPaginatorForQuery($query);
return $this->getCustomers($this->createCustomerQuery($customerQuery), $customerQuery);
}
return $paginator->getAll();
/**
* @param Query<Customer> $query
* @return Customer[]
*/
public function getCustomers(Query $query, CustomerQuery $customerQuery): array
{
/** @var array<Customer> $customers */
$customers = $query->execute();
$loader = new CustomerLoader($this->getEntityManager(), $customerQuery);
$loader->loadResults($customers);
return $customers;
}
public function deleteCustomer(Customer $delete, ?Customer $replace = null): void