Skip to content

Instantly share code, notes, and snippets.

@chukShirley
Last active December 26, 2015 12:59
Show Gist options
  • Save chukShirley/7155530 to your computer and use it in GitHub Desktop.
Save chukShirley/7155530 to your computer and use it in GitHub Desktop.
ZF2 Zend\Db nested select/subquery for pagination
/*
*
* Trying to produce the following sql string
*
* SELECT *
* FROM (SELECT ROWNUM() OVER(ORDER BY GAACCT) as rn, GAACCT as id, GADESC as name, GATYPE as type FROM ACCTSMST)
* WHERE rn between ? and ?
*
*/
$sql = new Sql($this->dbAdapter);
// Build subquery
$subselect = $sql->select('ACCTSMST');
$subselect->columns(array('rn'=>'ROWNUM() OVER (ORDER BY GAACCT)','id'=>'GAACCT','name'=>'GADESC','type'=>'GATYPE'));
// Build main query
$select=$sql->select()
->columns(array('*'))
->from($subselect)
->where("rn between ? and ?");
// Prepare statement
$statement = $sql->prepareStatementForSqlObject($select);
if (!$statement){
error_log("Prepare error:".db2_stmt_error().db2_stmt_errormsg());
}
// Execute statement
$rowset = $astatement->execute();
/*
* Getting the following error:
* Zend\Db\Sql\Exception\InvalidArgumentException
*
* File:
*
* /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/Db/Sql/Select.php:190
*
* Message:
*
* $table must be a string, array, or an instance of TableIdentifier
*
* Stack trace:
*
* #0 /www/ZF2Demo/module/Ledger/src/Model/AccountsTable.php(71): Zend\Db\Sql\Select->from(Object(Zend\Db\Sql\Select))
* #1 /www/ZF2Demo/module/Ledger/src/Controller/AccountsController.php(105): Ledger\Model\AccountsTable->fetchAll('1', '50', Array, Array)
* #2 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/Mvc/Controller/AbstractActionController.php(83): Ledger\Controller\AccountsController->fetchAllAction()
* #3 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
* #4 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
* #5 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
* #6 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
* #7 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
* #8 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
* #9 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
* #10 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
* #11 /usr/local/zendsvr6/var/libraries/Zend_Framework_2/2.2.2/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
* #12 /www/ZF2Demo/public/index.php(13): Zend\Mvc\Application->run()
* #13 {main}
*
*/
@seanfbrown
Copy link

Any luck? Running into similar problems with mysql late lookups...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment