Zend Framework

Binding parameters and results to PHP variables is broken

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0.0 RC1
  • Fix Version/s: 1.0.0 RC2
  • Component/s: Zend_Db
  • Labels:
    None

Description

The function bindParam(), bindValue(), and bindColumn() need some bugfixing. These are not working with any Zend_Db adapters currently.

Issue Links

Activity

Hide
Jeffrey Sambells added a comment -

From my testing this seems to be an issue with PDO and the way the execute wrapper is defined:

<?
/**

  • Executes a prepared statement.
    *
  • @param array $params OPTIONAL Values to bind to parameter placeholders.
  • @return bool
  • @throws Zend_Db_Statement_Exception
    */
    public function execute(array $params = array())
    Unknown macro: { try { return $this->_stmt->execute($params); } catch (PDOException $e) { require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception($e->getMessage()); } }

    ?>

if you execute the object without any parameters to the Zend execute method:

$stmt->execute()

$params is assigned an empty array. PDO them seems to use this empty array rather than the parameters from bindParam etc. if you change the function declaration to:

public function execute(array $params = null)

everything begins to work again as without an input it now passes NULL to the PDO execute method rather than an empty array.

Show
Jeffrey Sambells added a comment - From my testing this seems to be an issue with PDO and the way the execute wrapper is defined: <? /**
  • Executes a prepared statement. *
  • @param array $params OPTIONAL Values to bind to parameter placeholders.
  • @return bool
  • @throws Zend_Db_Statement_Exception */ public function execute(array $params = array())
    Unknown macro: { try { return $this->_stmt->execute($params); } catch (PDOException $e) { require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception($e->getMessage()); } }
    ?>
if you execute the object without any parameters to the Zend execute method: $stmt->execute() $params is assigned an empty array. PDO them seems to use this empty array rather than the parameters from bindParam etc. if you change the function declaration to: public function execute(array $params = null) everything begins to work again as without an input it now passes NULL to the PDO execute method rather than an empty array.
Hide
Jeffrey Sambells added a comment -

Also, the default NULL values for $type in bindValue() (and possibly others) is also causing issues:

public function bindValue($parameter, $value, $type = null)

If you bind a value without specifying the $type with a PDO::PARAM_* value then the value becomes null when executed.

I suggest changing it to:

public function bindValue($parameter, $value, $type = null)
{
if (is_string($parameter) && $parameter[0] != ':') { $parameter = ":$parameter"; }
try {
if(is_null($type)) { return $this->_stmt->bindValue($parameter, $value); }
return $this->_stmt->bindValue($parameter, $value, $type);
} catch (PDOException $e) { require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception($e->getMessage()); }
}

Show
Jeffrey Sambells added a comment - Also, the default NULL values for $type in bindValue() (and possibly others) is also causing issues: public function bindValue($parameter, $value, $type = null) If you bind a value without specifying the $type with a PDO::PARAM_* value then the value becomes null when executed. I suggest changing it to: public function bindValue($parameter, $value, $type = null) { if (is_string($parameter) && $parameter[0] != ':') { $parameter = ":$parameter"; } try { if(is_null($type)) { return $this->_stmt->bindValue($parameter, $value); } return $this->_stmt->bindValue($parameter, $value, $type); } catch (PDOException $e) { require_once 'Zend/Db/Statement/Exception.php'; throw new Zend_Db_Statement_Exception($e->getMessage()); } }
Hide
Bill Karwin added a comment -

Thanks for the help debugging, Jeffrey! That did it – at least for PDO adapters.

The fixes you suggest have been committed in revision 5048.

I'm going to leave this issue open for now, because I'd like to get it working across all adapters.

Show
Bill Karwin added a comment - Thanks for the help debugging, Jeffrey! That did it – at least for PDO adapters. The fixes you suggest have been committed in revision 5048. I'm going to leave this issue open for now, because I'd like to get it working across all adapters.
Hide
Bill Karwin added a comment -

This fix was included in 1.0.0 RC2, at least for PDO adapters.

I have opened ZF-1475, ZF-1476, ZF-1477 for these binding features in IBM DB2, Oracle, and Mysqli.

Show
Bill Karwin added a comment - This fix was included in 1.0.0 RC2, at least for PDO adapters. I have opened ZF-1475, ZF-1476, ZF-1477 for these binding features in IBM DB2, Oracle, and Mysqli.
Hide
Bill Karwin added a comment -

Link to ZF-1475.

Show
Bill Karwin added a comment - Link to ZF-1475.
Hide
Bill Karwin added a comment -

Link to ZF-1476.

Show
Bill Karwin added a comment - Link to ZF-1476.
Hide
Bill Karwin added a comment -

Link to ZF-1477.

Show
Bill Karwin added a comment - Link to ZF-1477.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: