Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like Zend Framework 2.1.4 has support to specify a quantifier. This enables you to use the SQL_CALC_FOUND_ROWS in a select object. One thing I did find tricky to work around is that Zend's Zend\Db\Sql\Select class will not generate the correct SQL for you if you did not specify a table. This becomes and issue when executing the subsequent select to retrieve the FOUND_ROWS(). I've updated your code below to include what I would use. I've merge my project implementation into your code, so if something does not work, its probably because I mistype something, but overall it works for me (not as desirable as I would want).</p> <pre><code>use Zend\Db\Sql\Expression; use Zend\Db\Sql\Select; class ProductTable { protected $tableGateway; /** * Set database gateway * * @param TableGateway $tableGateway - database connection * @return void */ public function __construct(TableGateway $tableGateway) { $this-&gt;tableGateway = $tableGateway; } /** * Fetch all products * * @param integer $page - page of records * @param integer $perpage - records per page * @return void */ public function fetchAll($page = 1, $perpage = 18) { $result = $this-&gt;tableGateway-&gt;select(function (Select $select) use ($page, $perpage) { $select -&gt;quantifier(new Expression('SQL_CALC_FOUND_ROWS')) -&gt;limit($perpage) -&gt;offset(($page - 1) * $perpage); }); /* retrieve the sql object from the table gateway */ $sql = $this-&gt;tableGateway-&gt;getSql(); /* create an empty select statement passing in some random non-empty string as the table. need this because Zend select statement will generate an empty SQL if the table is empty. */ $select = new Select(' '); /* update the select statement specification so that we don't incorporate the FROM clause */ $select-&gt;setSpecification(Select::SELECT, array( 'SELECT %1$s' =&gt; array( array(1 =&gt; '%1$s', 2 =&gt; '%1$s AS %2$s', 'combinedby' =&gt; ', '), null ) )); /* specify the column */ $select-&gt;columns(array( 'total' =&gt; new Expression("FOUND_ROWS()") )); /* execute the select and extract the total */ $statement = $sql-&gt;prepareStatementForSqlObject($select); $result2 = $statement-&gt;execute(); $row = $result2-&gt;current(); $total = $row['total']'; /* TODO: need to do something with the total? */ return $result; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload