Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #673 from Seb33300/fix-recordsTotal
Browse files Browse the repository at this point in the history
Fix recordsTotal count
  • Loading branch information
Seb33300 authored Sep 4, 2017
2 parents d5edd69 + f0ec702 commit 90cc764
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
1 change: 0 additions & 1 deletion Resources/doc/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ if ($isAjax) {
$responseService->setDatatable($datatable);

$datatableQueryBuilder = $responseService->getDatatableQueryBuilder();
$datatableQueryBuilder->buildQuery();

$datatableQueryBuilder->useQueryCache(true); // (1)
$datatableQueryBuilder->useCountQueryCache(true); // (2)
Expand Down
6 changes: 1 addition & 5 deletions Resources/doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,7 @@ public function indexAction(Request $request)
if ($isAjax) {
$responseService = $this->get('sg_datatables.response');
$responseService->setDatatable($datatable);

$datatableQueryBuilder = $responseService->getDatatableQueryBuilder();
$datatableQueryBuilder->buildQuery();

//dump($datatableQueryBuilder->getQb()->getDQL()); die();
$responseService->getDatatableQueryBuilder();

return $responseService->getResponse();
}
Expand Down
3 changes: 0 additions & 3 deletions Resources/doc/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public function indexAction(Request $request)
$responseService->setDatatable($datatable);

$datatableQueryBuilder = $responseService->getDatatableQueryBuilder();
$datatableQueryBuilder->buildQuery();

//dump($datatableQueryBuilder->getQb()->getDQL()); die();

/** @var QueryBuilder $qb */
$qb = $datatableQueryBuilder->getQb();
Expand Down
68 changes: 44 additions & 24 deletions Response/DatatableQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function __construct(array $requestParams, DatatableInterface $datatable)
$this->entityShortName = $this->getEntityShortName($this->metadata);
$this->rootEntityIdentifier = $this->getIdentifier($this->metadata);

$this->qb = $this->em->createQueryBuilder();
$this->qb = $this->em->createQueryBuilder()->from($this->entityName, $this->entityShortName);
$this->accessor = PropertyAccess::createPropertyAccessor();

$this->columns = $datatable->getColumnBuilder()->getColumns();
Expand Down Expand Up @@ -308,16 +308,12 @@ private function initColumnArrays()
/**
* Build query.
*
* @deprecated No longer used by internal code.
*
* @return $this
*/
public function buildQuery()
{
$this->setSelectFrom();
$this->setJoins($this->qb);
$this->setWhere($this->qb);
$this->setOrderBy();
$this->setLimit();

return $this;
}

Expand Down Expand Up @@ -345,27 +341,45 @@ public function setQb($qb)
return $this;
}

/**
* Get the built qb.
*
* @return QueryBuilder
*/
public function getBuiltQb()
{
$qb = clone $this->qb;

$this->setSelectFrom($qb);
$this->setJoins($qb);
$this->setWhere($qb);
$this->setOrderBy($qb);
$this->setLimit($qb);

return $qb;
}

//-------------------------------------------------
// Private/Public - Setup query
//-------------------------------------------------

/**
* Set select from.
*
* @param QueryBuilder $qb
*
* @return $this
*/
private function setSelectFrom()
private function setSelectFrom(QueryBuilder $qb)
{
foreach ($this->selectColumns as $key => $value) {
if (false === empty($key)) {
$this->qb->addSelect('partial '.$key.'.{'.implode(',', $value).'}');
$qb->addSelect('partial '.$key.'.{'.implode(',', $value).'}');
} else {
$this->qb->addSelect($value);
$qb->addSelect($value);
}
}

$this->qb->from($this->entityName, $this->entityShortName);

return $this;
}

Expand Down Expand Up @@ -426,7 +440,7 @@ private function setWhere(QueryBuilder $qb)
}

if ($orExpr->count() > 0) {
$qb->where($orExpr);
$qb->andWhere($orExpr);
}
}

Expand Down Expand Up @@ -465,9 +479,11 @@ private function setWhere(QueryBuilder $qb)
* Ordering.
* Construct the ORDER BY clause for server-side processing SQL query.
*
* @param QueryBuilder $qb
*
* @return $this
*/
private function setOrderBy()
private function setOrderBy(QueryBuilder $qb)
{
if (isset($this->requestParams['order']) && count($this->requestParams['order'])) {
$counter = count($this->requestParams['order']);
Expand All @@ -480,7 +496,7 @@ private function setOrderBy()
$columnName = $this->orderColumns[$columnIdx];
$orderDirection = $this->requestParams['order'][$i]['dir'];

$this->qb->addOrderBy($columnName, $orderDirection);
$qb->addOrderBy($columnName, $orderDirection);
}
}
}
Expand All @@ -492,14 +508,16 @@ private function setOrderBy()
* Paging.
* Construct the LIMIT clause for server-side processing SQL query.
*
* @param QueryBuilder $qb
*
* @return $this
* @throws Exception
*/
private function setLimit()
private function setLimit(QueryBuilder $qb)
{
if (true === $this->features->getPaging() || null === $this->features->getPaging()) {
if (isset($this->requestParams['start']) && DatatableQueryBuilder::DISABLE_PAGINATION != $this->requestParams['length']) {
$this->qb->setFirstResult($this->requestParams['start'])->setMaxResults($this->requestParams['length']);
$qb->setFirstResult($this->requestParams['start'])->setMaxResults($this->requestParams['length']);
}
} elseif ($this->ajax->getPipeline() > 0) {
throw new Exception('DatatableQueryBuilder::setLimit(): For disabled paging, the ajax Pipeline-Option must be turned off.');
Expand All @@ -515,9 +533,10 @@ private function setLimit()
*/
public function execute()
{
$query = $this->qb->getQuery();
$query->setHydrationMode(Query::HYDRATE_ARRAY)
->useQueryCache($this->useQueryCache);
$qb = $this->getBuiltQb();

$query = $qb->getQuery();
$query->setHydrationMode(Query::HYDRATE_ARRAY)->useQueryCache($this->useQueryCache);
call_user_func_array([$query, 'useResultCache'], $this->useResultCacheArgs);

return $query;
Expand All @@ -530,15 +549,16 @@ public function execute()
*/
public function getCountAllResults()
{
$qb = $this->em->createQueryBuilder();
$qb = clone $this->qb;
$qb->select('count(distinct '.$this->entityShortName.'.'.$this->rootEntityIdentifier.')');
$qb->from($this->entityName, $this->entityShortName);
$this->setJoins($qb);

$query = $qb->getQuery();
$query->useQueryCache($this->useCountQueryCache);
call_user_func_array([$query, 'useResultCache'], $this->useCountResultCacheArgs);

return !$qb->getDQLPart('groupBy') ?
(int) $query->getSingleScalarResult()
return !$qb->getDQLPart('groupBy')
? (int) $query->getSingleScalarResult()
: count($query->getResult());
}

Expand Down

0 comments on commit 90cc764

Please sign in to comment.