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


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.


Modified Zend_Db_Adapter_Abstract class with new method getStatement.

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

Fixed typo ;)

Please categorize/fix as needed.

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.

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

Will evaluate within 2 weeks

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.

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.

$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);