ZF-3470: Zend_Db_Statement_Pdo::setFetchMode does only except one parameter, while for PDO::FETCH_CLASS 2 (or 3) are needed


For Zend_Db_Statement_Pdo::setFetchMode multiple arguments could be required, when working with PDO::FETCH_CLASS for example. The current implementation does not support this feature since only the fetchmode can be set.

h3. Te reproduce

class FooClass {} 

$stmt = $db->query ( 'SELECT * FROM foo' );
$stmt->setFetchMode ( PDO::FETCH_CLASS, 'FooClass' );

Which results in: Zend_Db_Statement_Exception: SQLSTATE[HY000]: General error: No fetch class specified in ...

h3. Suggested fix

public function setFetchMode($mode)
     $args = func_get_args ();  

     $this->_fetchMode = $mode;

     try {
          return call_user_func_array ( array ( $this->_stmt, 'setFetchMode' ), $args );
     } catch (PDOException $e) {
          require_once 'Zend/Db/Statement/Exception.php';
          throw new Zend_Db_Statement_Exception($e->getMessage());


Instead of adding PDO specific features to the Statement general API, we have decided to add a method called getDriverStatement() which will be able to return the internal statement handler.

In the case of PDO, it will return PDOStatement, and you'll be able to call setFetchMode() on that object:

$stmt->getDriverStatement()->setFetchMode(....); // PDO specific method.

This helps keep the API consistent across all adapters/drivers.

This won't work. Zend_Db_Select has method query. This method accepts parameter $fetchMode, which if not given is initialized to Zend_Db_Adapter_Abstract->getFetchMode(). Later, setFetchMode is called with this parameter. Point is, if we use PDO, which REQUIRES 2 or 3 arguments to setFetchMode, this will break no matter what fetch mode is set beforehand.

Sorry, previous comment was not specific enough. PDO with FETCH_CLASS requires 2 or 3 arguments to setFetchMode. Still, PDO and $fetchMode==FETCH_CLASS will break, no matter if fetchMode was added with getDriverStatement or not.