Skip to content

Commit

Permalink
Merge pull request #20 from Raistlfiren/hotfix-19
Browse files Browse the repository at this point in the history
Fix for issues #19
  • Loading branch information
xiaohutai authored Jul 22, 2016
2 parents b2fe711 + 40c4096 commit 98bee8b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 33 deletions.
5 changes: 0 additions & 5 deletions src/Action/SearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public function handle($contentType = null, Request $request, ParameterCollectio

$queryParameters = array_merge($parameters->getQueryParameters(), $parameters->getParametersByType('search'));

if ($contentType === null) {
//Remove search order off of id, since ambigious...
unset($queryParameters['order']);
}

/** @var PagingResultSet $results */
$set = $this->query
->getContent($baselink, $queryParameters);
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/JSONAPIConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function grabParameters($converter, Request $request)

$parameters['page']['size'] = $request->query->get('page[size]', false, true);
$parameters['page']['number'] = $request->query->get('page[number]', false, true);
$parameters['order'] = $request->query->get('sort', false);
$parameters['sort'] = $request->query->get('sort', false);
$parameters['filters'] = $request->query->get('filter', false);
$parameters['contains'] = $request->query->get('contains', false);
$parameters['includes'] = $request->query->get('include', false);
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/Parameter/ParameterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getQueryParameters()

//Loop through the parameters and add them to an array
foreach ($this as $key => $value) {
if (in_array($key, ['order', 'filters', 'contains', 'page'])) {
if (in_array($key, ['sort', 'filters', 'contains', 'page'])) {
$queryParameters = array_merge($value->getParameter(), $queryParameters);
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Storage/Query/Handler/PagingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class PagingHandler
public function __invoke(ContentQueryParser $contentQuery)
{
$set = new PagingResultSet();
$cleanSearchQuery = $contentQuery->getService('search');

foreach ($contentQuery->getContentTypes() as $contenttype) {
//Find out if we are searching or just doing a simple query
if ($searchParam = $contentQuery->getParameter('filter')) {
$query = $contentQuery->getService('search');
$query = clone $cleanSearchQuery;
} else {
$query = $contentQuery->getService('select');
}
Expand All @@ -40,7 +41,7 @@ public function __invoke(ContentQueryParser $contentQuery)

//Get Page from the new directive handler that allows pagination
$paginate = $contentQuery->getDirective('paginate');

//Set the default limitto the pagination size, since it defaults to null
$contentQuery->setDirective('limit', $paginate->getSize());

Expand All @@ -58,24 +59,25 @@ public function __invoke(ContentQueryParser $contentQuery)
$qb = clone $query->getQueryBuilder();

$query2
->resetQueryParts(['groupBy', 'maxResults', 'firstResult'])
->resetQueryParts(['maxResults', 'firstResult', 'orderBy'])
->setFirstResult(null)
->setMaxResults(null)
->select("COUNT(*) as total");

$totalItems = $repo->findResult($query2);
$totalItems = count($repo->findResults($query2));

$result = $repo->findResults($qb);
if ($result) {
$set->add($result, $contenttype);
$set->setTotalResults($totalItems);
$set->setTotalResults((int) $totalItems);

/** @var Page $page */
$page = $contentQuery->getDirective('paginate');
$set->setTotalPages($totalItems, $page->getSize());
}

return $set;
}

return $set;

}
}
4 changes: 2 additions & 2 deletions src/Storage/Query/PagingResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PagingResultSet extends QueryResultset
{

/** @var int $totalResults */
protected $totalResults;
protected $totalResults = 0;

/** @var int $totalPages */
protected $totalPages;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function getTotalResults()
*/
public function setTotalResults($totalResults)
{
$this->totalResults = $totalResults;
$this->totalResults += $totalResults;
return $this;
}
}
17 changes: 0 additions & 17 deletions src/Storage/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,4 @@ public function findResults(QueryBuilder $query)
return false;
}
}

/**
* Since the queries are built already, we don't need to run all of the other mappings
* before. Now we can just fetch the results of the query instead of running everything else.
* This fetches a single result (count) and returns the total.
* @param QueryBuilder $query
* @return bool|mixed
*/
public function findResult(QueryBuilder $query)
{
$result = $query->execute()->fetch();
if ($result) {
return $result['total'];
} else {
return false;
}
}
}

0 comments on commit 98bee8b

Please sign in to comment.