Skip to content

Commit

Permalink
Use oficial Doctrine Paginator and HavingNumberRange filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-teixeira committed Aug 18, 2014
1 parent f72a402 commit 9c209cd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
49 changes: 49 additions & 0 deletions Grid/Filter/Operator/HavingNumberRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace PedroTeixeira\Bundle\GridBundle\Grid\Filter\Operator;

/**
* HavingNumberRange
*/
class HavingNumberRange extends OperatorAbstract
{
/**
* @param array $value
*
* @throws \Exception
*/
public function execute($value)
{
if (!is_array($value) || count($value) != 2) {
throw new \Exception('Value for number range comparison should have to values');
}

$queryBuilder = $this->getQueryBuilder();

if (!empty($value[0])) {
$queryBuilder->andHaving(
$queryBuilder->expr()->gte(
$this->getIndex(),
":{$this->getIndexClean()}_1"
)
)
->setParameter(
":{$this->getIndexClean()}_1",
(float) $value[0]
);
}

if (!empty($value[1])) {
$queryBuilder->andHaving(
$queryBuilder->expr()->lte(
$this->getIndex(),
":{$this->getIndexClean()}_2"
)
)
->setParameter(
":{$this->getIndexClean()}_2",
(float) $value[1]
);
}
}
}
4 changes: 2 additions & 2 deletions Grid/GridAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;

use DoctrineExtensions\Paginate\Paginate;
use Doctrine\ORM\Tools\Pagination\Paginator;

use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -325,7 +325,7 @@ public function getData()

// Don't process grid for export
if (!$this->isExport()) {
$totalCount = Paginate::count($this->getQueryBuilder()->getQuery());
$totalCount = count(new Paginator($this->getQueryBuilder()->getQuery()));

$totalPages = ceil($totalCount / $limit);
$totalPages = ($totalPages <= 0 ? 1 : $totalPages);
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ pedro-teixeira/grid-bundle
Requirements
------------

1. [Doctrine Extensions](https://github.com/beberlei/DoctrineExtensions)
2. [Twitter Bootstrap](http://twitter.github.com/bootstrap/) (not mandatory)
1. [Twitter Bootstrap](http://twitter.github.com/bootstrap/) (not mandatory)
* If you choose to don't use Twitter Bootstrap, it'll be necessary to create your own style.
3. [jQuery Bootstrap Datepicker](http://www.eyecon.ro/bootstrap-datepicker/) (not mandatory)
2. [jQuery Bootstrap Datepicker](http://www.eyecon.ro/bootstrap-datepicker/) (not mandatory)
* If you choose to don't use Bootstrap Datepicker, please disable the datepicker as default in the configuration, "use_datepicker".


Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"require":{
"php":">=5.3.2",
"symfony/framework-bundle":"~2",
"doctrine/orm":"~2",
"beberlei/DoctrineExtensions":"@dev"
"doctrine/orm":"~2.2"
},
"autoload":{
"psr-0":{
Expand Down

0 comments on commit 9c209cd

Please sign in to comment.