Unverified Commit 5ab5b35a authored by Progi1984's avatar Progi1984 Committed by GitHub
Browse files

Merge pull request #26221 from atomiix/fix/24911

Fix SQL query in OrderQueryBuilder
parents 36cdc6b3 16158620
......@@ -116,13 +116,8 @@ final class OrderQueryBuilder implements DoctrineQueryBuilderInterface
public function getCountQueryBuilder(SearchCriteriaInterface $searchCriteria)
{
$qb = $this->getBaseQueryBuilder($searchCriteria->getFilters());
if (isset($searchCriteria->getFilters()['new'])) {
$this->addNewCustomerField($qb->addSelect('o.id_order as o_id_order'));
$qb = $this->applyNewCustomerFilter($qb, $searchCriteria->getFilters());
$qb->select('count(o_id_order)');
} else {
$qb->select('count(o.id_order)');
}
$qb = $this->applyNewCustomerFilter($qb, $searchCriteria->getFilters());
$qb->select('count(o.id_order)');
return $qb;
}
......@@ -233,20 +228,27 @@ final class OrderQueryBuilder implements DoctrineQueryBuilderInterface
}
/**
* @param QueryBuilder $qb
* Returns the subquery defining if a customer is new or not
*/
private function addNewCustomerField(QueryBuilder $qb)
private function getNewCustomerSubSelect(): string
{
$newCustomerSubSelect = $this->connection
return $this->connection
->createQueryBuilder()
->select('so.id_order')
->select('IF(count(so.id_order) > 0, 0, 1)')
->from($this->dbPrefix . 'orders', 'so')
->where('so.id_customer = o.id_customer')
->andWhere('so.id_order < o.id_order')
->setMaxResults(1)
->getSQL()
;
}
$qb->addSelect('IF ((' . $newCustomerSubSelect->getSQL() . ') > 0, 0, 1) AS new');
/**
* @param QueryBuilder $qb
*/
private function addNewCustomerField(QueryBuilder $qb): void
{
$qb->addSelect('(' . $this->getNewCustomerSubSelect() . ') AS new');
}
/**
......@@ -269,11 +271,8 @@ final class OrderQueryBuilder implements DoctrineQueryBuilderInterface
return $qb;
}
$builder = $this->connection
->createQueryBuilder()
->select('*')
->from('(' . $qb->getSQL() . ') tmp_table')
->andWhere('new = :new')
$builder = $qb
->andWhere('(' . $this->getNewCustomerSubSelect() . ') = :new')
->setParameter('new', $filters['new'])
;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment