Zend_Db::insert()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Zend_Db::insert()

Diego Malatesta
Hello,

I'm a lurker of this ml and a selft taught php developer. I'm
absolutley not a pro so please forgive me if I say stupid things =)

Playing with Zend_Db I found that using the insert method as described
by the manual results in the following error:

"Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'
in /var/www/zend/lib/Zend/Db/Adapter/Abstract.php:153"

the error says that I'm passing a wrong parameter to the execute
method called in the query method:

______________
    public function query($sql, $bind = array())
    {
        // connect to the database if needed
        $this->_connect();

        // is the $sql a Zend_Db_Select object?
        if ($sql instanceof Zend_Db_Select) {
            $sql = $sql->__toString();
        }

        // prepare and execute the statement with profiling
        $stmt = $this->prepare($sql);
        $q = $this->_profiler->queryStart($sql);
        //ERROR HERE
        $stmt->execute((array) $bind);
        //----------------------
        $this->_profiler->queryEnd($q);

        // return the results embedded in the prepared statement object
        $stmt->setFetchMode($this->_fetchMode);
        return $stmt;
    }
_______________

so I read the PDO documentation and found that execute() accepts only:
1. No parameters
2. array( ':placeholder1' => 'value1', ':placeholder2' => 'value2' )
3. array('value1', 'value2')

our case is the n°3 but debugging I found that the $bind array is
still the one I've passed to the insert() method (
array('column_1_name' => 'value1'....)

I solved by adding an array_values here:

____________
        $q = $this->_profiler->queryStart($sql);
        //ERROR WAS HERE
        $stmt->execute(array_values($bind));
        //----------------------
        $this->_profiler->queryEnd($q);
____________

I dont know if this is the best way to solve the issue (probably not)
or if it causes other problems. Or maybe I'm doing something wrong
when calling the insert() method...

I searched through the open tickets on the fw site and on the php arch
forum but I didnt find anything related...If the problem is already
solved, I apologize for the useless mail =)


Diego Malatesta
(Part time PHP noob =)