Example #1
0
 /**
  * @param Select $select
  * @param Pagination $pagination
  * @throws Exception\RuntimeException
  * @return array
  */
 protected function executeSelect(Select $select, Pagination $pagination = null)
 {
     $selectState = $select->getRawState();
     if ((string) $selectState['table'] != (string) $this->table) {
         throw new Exception\RuntimeException('The table name of the provided select object must match that of the table');
     }
     if ($pagination) {
         $counter = clone $select;
         $counter->reset(Select::COLUMNS)->reset(Select::ORDER)->reset(Select::LIMIT)->reset(Select::OFFSET);
         if (strtoupper($select->getRawState($select::QUANTIFIER)) == Select::QUANTIFIER_DISTINCT) {
             $columns = $select->getRawState(Select::COLUMNS);
             $counter->columns("COUNT({$select->getRawState(Select::QUANTIFIER)} {$columns[0]}) as total");
         } else {
             $counter->columns('COUNT(*) as total');
         }
         $row = $this->sql->prepareStatement($counter)->execute()->current();
         $pagination->setRecordCount($row['total']);
         $size = $pagination->getPageSize();
         $currentPage = $pagination->getCurrentPage();
         $select->limit($size)->offset(($currentPage - 1) * $size);
     }
     // prepare and execute
     $statement = $this->sql->prepareStatement($select);
     $rowset = array();
     foreach ($statement->execute() as $row) {
         $rowset[] = $row;
     }
     return $rowset;
 }