diff --git a/README.bak b/README.bak deleted file mode 100644 index e6c7807..0000000 --- a/README.bak +++ /dev/null @@ -1,84 +0,0 @@ -# Datatables - -[![Latest Version on Packagist][ico-version]][link-packagist] -[![Software License][ico-license]][link-license] -[![Build Status][ico-travis]][link-travis] -[![Coverage Status][ico-scrutinizer]][link-scrutinizer] -[![Quality Score][ico-code-quality]][link-code-quality] -[![Total Downloads][ico-downloads]][link-downloads] - -Datatables is a package that uses [Doctrine](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest) or [Eloquent](https://laravel.com/docs/master/eloquent) to generate a [server-side AJAX result](https://datatables.net/examples/data_sources/server_side.html) to a [Datatable](https://datatables.net/) instance with little to no configuration. - -## Installation - -Install `Datatables` via [Composer](https://getcomposer.org/): - -``` bash -$ composer require rougin/datatables -``` - -## Basic Usage - -### Doctrine - -``` php -use Rougin\Datatables\Legacy\DoctrineBuilder; - -$entity = 'Acme\Doctrine\Models\User'; - -$builder = new DoctrineBuilder($manager, $entity, $_GET); - -header('Content-Type: application/json'); - -echo json_encode($builder->make()); -``` - -**NOTE**: `$manager` must return an instance of `Doctrine\ORM\EntityManager`. See [DoctrineBuilderTest::setUp](https://github.com/rougin/datatables/blob/master/tests/DoctrineBuilderTest.php#L26) for the sample implementation. - -### Eloquent - -``` php -use Rougin\Datatables\Legacy\EloquentBuilder; - -$model = 'Acme\Eloquent\Models\UserModel'; - -$builder = new EloquentBuilder($model, $_GET); - -header('Content-Type: application/json'); - -echo json_encode($builder->make()); -``` - -## Changelog - -Please see [CHANGELOG][link-changelog] for more information what has changed recently. - -## Testing - -``` bash -$ composer test -``` - -## Credits - -- [All contributors][link-contributors] - -## License - -The MIT License (MIT). Please see [LICENSE][link-license] for more information. - -[ico-code-quality]: https://img.shields.io/scrutinizer/g/rougin/datatables.svg?style=flat-square -[ico-downloads]: https://img.shields.io/packagist/dt/rougin/datatables.svg?style=flat-square -[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square -[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/rougin/datatables.svg?style=flat-square -[ico-travis]: https://img.shields.io/travis/rougin/datatables/master.svg?style=flat-square -[ico-version]: https://img.shields.io/packagist/v/rougin/datatables.svg?style=flat-square - -[link-changelog]: https://github.com/rougin/datatables/blob/master/CHANGELOG.md -[link-code-quality]: https://scrutinizer-ci.com/g/rougin/datatables -[link-contributors]: https://github.com/rougin/datatables/contributors -[link-downloads]: https://packagist.org/packages/rougin/datatables -[link-license]: https://github.com/rougin/datatables/blob/master/LICENSE.md -[link-packagist]: https://packagist.org/packages/rougin/datatables -[link-scrutinizer]: https://scrutinizer-ci.com/g/rougin/datatables/code-structure -[link-travis]: https://travis-ci.org/rougin/datatables \ No newline at end of file diff --git a/README.md b/README.md index 4d991c8..a800a79 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,8 @@ use Rougin\Datatables\Table; interface SourceInterface { /** - * Returns the total items after filter. + * Returns the total items after filter. If no filters + * are defined, the value should be same with getTotal. * * @return integer */ diff --git a/src/Source/PdoSource.php b/src/Source/PdoSource.php index d1a64aa..090cdb7 100644 --- a/src/Source/PdoSource.php +++ b/src/Source/PdoSource.php @@ -48,33 +48,14 @@ public function __construct(\PDO $pdo) } /** - * TODO: If no filters, the value should be same with getTotal. - * - * Returns the total items after filter. + * Returns the total items after filter. If no filters + * are defined, the value should be same with getTotal. * * @return integer */ public function getFiltered() { - // Reset values prior creating query --- - $this->values = array(); - // ------------------------------------- - - $table = $this->table->getName() . ' '; - - $query = 'SELECT COUNT(*) FROM ' . $table; - - $query .= $this->setWhereQuery() . ' '; - - /** @var \PDOStatement */ - $stmt = $this->pdo->prepare($query); - - $stmt->execute($this->values); - - /** @var integer */ - $total = $stmt->fetch(\PDO::FETCH_COLUMN); - - return (int) $total; + return $this->getTotalItems(true); } /** @@ -137,27 +118,13 @@ public function getItems() } /** - * TODO: Merge logic with getFiltered. - * * Returns the total items from the source. * * @return integer */ public function getTotal() { - $table = $this->table->getName(); - - $query = 'SELECT COUNT(*) FROM ' . $table; - - /** @var \PDOStatement */ - $stmt = $this->pdo->prepare($query); - - $stmt->execute(); - - /** @var integer */ - $total = $stmt->fetch(\PDO::FETCH_COLUMN); - - return (int) $total; + return $this->getTotalItems(); } /** @@ -188,6 +155,37 @@ public function setTable(Table $table) return $this; } + /** + * @param boolean $filter + * + * @return integer + */ + protected function getTotalItems($filter = false) + { + // Reset values prior creating query --- + $this->values = array(); + // ------------------------------------- + + $table = $this->table->getName() . ' '; + + $query = 'SELECT COUNT(*) FROM ' . $table; + + if ($filter) + { + $query .= $this->setWhereQuery() . ' '; + } + + /** @var \PDOStatement */ + $stmt = $this->pdo->prepare(trim($query)); + + $stmt->execute($this->values); + + /** @var integer */ + $total = $stmt->fetch(\PDO::FETCH_COLUMN); + + return (int) $total; + } + /** * @return string */ diff --git a/src/Source/SourceInterface.php b/src/Source/SourceInterface.php index 0335b5d..f37d45c 100644 --- a/src/Source/SourceInterface.php +++ b/src/Source/SourceInterface.php @@ -13,7 +13,8 @@ interface SourceInterface { /** - * Returns the total items after filter. + * Returns the total items after filter. If no filters + * are defined, the value should be same with getTotal. * * @return integer */