It looks like Zend Framework 2.1.4 has support for specifying a quantifier. This allows you to use SQL_CALC_FOUND_ROWS in the selected object. The only thing that seemed complicated to me was that the Zend Zend \ Db \ Sql \ Select class would not generate the correct SQL for you if you did not specify a table. This becomes and arises during the subsequent selection to retrieve FOUND_ROWS (). I updated your code below to include what I will use. I combined my project implementation into my code, so if something doesnโt work, maybe because Iโm mistaken for something, but overall it works for me (not as desirable as I would like).
use Zend\Db\Sql\Expression; use Zend\Db\Sql\Select; class ProductTable { protected $tableGateway; public function __construct(TableGateway $tableGateway) { $this->tableGateway = $tableGateway; } public function fetchAll($page = 1, $perpage = 18) { $result = $this->tableGateway->select(function (Select $select) use ($page, $perpage) { $select ->quantifier(new Expression('SQL_CALC_FOUND_ROWS')) ->limit($perpage) ->offset(($page - 1) * $perpage); }); $sql = $this->tableGateway->getSql(); $select = new Select(' '); $select->setSpecification(Select::SELECT, array( 'SELECT %1$s' => array( array(1 => '%1$s', 2 => '%1$s AS %2$s', 'combinedby' => ', '), null ) )); $select->columns(array( 'total' => new Expression("FOUND_ROWS()") )); $statement = $sql->prepareStatementForSqlObject($select); $result2 = $statement->execute(); $row = $result2->current(); $total = $row['total']'; /* TODO: need to do something with the total? */ return $result; }
}
source share