ZF-2607: Add method to Zend_Db_Adapter_Abstract to retrieve last used statement object

Issue Type: Improvement Created: 2008-02-09T19:21:34.000+0000 Last Updated: 2009-08-28T02:48:16.000+0000 Status: Resolved Fix version(s): - 1.10.0 (27/Jan/10)

Reporter: Ota Mares (ota) Assignee: Ralph Schindler (ralph) Tags: - Zend_Db

Related issues: - ZF-7707

Attachments: - Abstract.php


Add method to Zend_Db_Adapter_Abstract to retrieve last used statement object.

This would allow users to call the errorCode and errorInfo PDO methods if a query fails and additional infos are needed.


Posted by Ota Mares (ota) on 2008-02-09T19:27:16.000+0000

Modified Zend_Db_Adapter_Abstract class with new method getStatement.

The PDO statement gets safed when the query method is executed (see line 415).

Posted by Ota Mares (ota) on 2008-02-09T19:31:17.000+0000

Fixed typo ;)

Posted by Wil Sinclair (wil) on 2008-03-25T20:43:55.000+0000

Please categorize/fix as needed.

Posted by Benjamin Eberlei (beberlei) on 2008-09-05T11:30:19.000+0000

It would also be possible to add the PDOStatement/Exception/ErrorInfo as additional parameters to the statement exception. and make it retrievable by additional getPDOException method or something.

Posted by Wil Sinclair (wil) on 2009-01-06T10:50:41.000+0000

No action on this issue for too long. I'm reassigning to Ralph for re-evaluation and categorization.

Posted by Ralph Schindler (ralph) on 2009-01-10T11:12:13.000+0000

Will evaluate within 2 weeks

Posted by Ralph Schindler (ralph) on 2009-08-27T15:55:39.000+0000

Keeping a reference to the last statement has the potential of creating a memory leak in Zend_Db, since Zend_Db_Adapter::query() actually gives you the statement, its the job of the developer to keep track of it.

There should be no relation between the adapter and the statement. Please reopen if you have a use case that might be considered.

Posted by Ota Mares (ota) on 2009-08-28T02:47:09.000+0000

Please look at the following code example. There is no other way to check what kind of error i have than comparing the string value of the exception (And thats an ugly solution to solve it). The Zend_Db_Adapter_Abstract::insert() method does not return the statement object and there is no way to retrieve it, thats why i asked for a get method.

<pre class="highlight">
$trys = 5;

do {
    try {
        // just a dumbed down example
        $id = 12345;
        $db->insert('table', array('id' => $id));
    catch(Zend_Db_Statement_Exception $e) {

        // duplicate value error, regenerate id and retry insert
        if (false !== strpos($e->getMessage(), '1062 Duplicate entry') && $trys > 0) {
        // general mysql error, throw the exception again
        else {
            throw $e;
// do until trys are used up
while($trys > 0);

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.