123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace Cube\Paginator\Adapter;
- use Cube\Db\Select,
- Cube\Db\Expr;
- class DbSelect implements AdapterInterface
- {
-
- protected $_select;
-
- protected $_count = null;
-
- public function __construct(Select $select)
- {
- $this->_select = $select;
- }
-
- public function getItems($offset, $itemCountPerPage)
- {
- $this->_select->limit($itemCountPerPage, $offset);
- return $this->_select->query()->fetchAll();
- }
-
- public function count()
- {
- if ($this->_count === null) {
- $select = clone $this->_select;
- $union = $select->getPart(Select::UNION);
- $having = $select->getPart(Select::HAVING);
- if (count($union) > 0 || count($having) > 0) {
- $stmt = $select->query();
- $this->_count = count($stmt->fetchAll());
- }
- else {
- $select->reset(Select::COLUMNS)
- ->reset(Select::ORDER);
- $select->columns(array('nb_rows' => new Expr('count(*)')));
- $stmt = $select->query();
- if ($select->getPart(Select::GROUP)) {
- $this->_count = count($stmt->fetchAll());
- }
- else {
- $this->_count = (integer) $stmt->fetchColumn('nb_rows');
- }
- }
- }
- return $this->_count;
- }
- }
|