The author uses:
createRow() and save() approach for inserting a record. I found that this
way does not return the ID with >lastInsertId(). It returns 0

But if I use the insert($data) approach, it returns the ID correctly.

so can somebody explains me why the first method does not return the ID ?
I would really prefer the save() method, because it allows me to dynamically
add or update rows. The only issue that I need the lastInsertId()

I'm getting value 0. The record is actually created in the database.
Do you havew any clue what's going on ? Why I'm =getting value 0 ?
How can I trace the problem ?

I'm using InnoDB engine. PDO connection.

Code:

<?php

class Model_Bug extends Zend_Db_Table_Abstract
{
        protected $_name = 'bugs';
        protected $_primary = 'id';

        public function createBug($name, $email,$date, $url, $description,
$priority, $status)
        {
                
                $dateObject = new Zend_Date($date);
                $row = $this->createRow();
                
                $row->author = $name;
                $row->email = $email;
                $dateObject = new Zend_Date($date);
                $row->date = $dateObject->get(Zend_Date::TIMESTAMP);
                $row->url = $url;
                $row->description = $description;
                $row->priority = $priority;
                
                // save the new row
                $row->save();
                // now fetch the id of the row you just created and return it
                $id = $this->_db->lastInsertId();
                print $id; exit;

        }
}

THE SQL for creating table:

Code:

CREATE TABLE `bugs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`author` varchar(250) DEFAULT NULL,
`email` varchar(250) DEFAULT NULL,
`date` int(11) DEFAULT NULL,
`url` varchar(250) DEFAULT NULL,
`description` text,
`priority` varchar(50) DEFAULT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
)


Default
I'm getting value 0. The record is actually created in the database.
Do you havew any clue what's going on ? Why I'm =getting value 0 ?
How can I trace the problem ?

I'm using InnoDB engine. PDO connection.

Code:

<?php

class Model_Bug extends Zend_Db_Table_Abstract
{
        protected $_name = 'bugs';
        protected $_primary = 'id';

        public function createBug($name, $email,$date, $url, $description,
$priority, $status)
        {
                
                $dateObject = new Zend_Date($date);
                $row = $this->createRow();
                
                $row->author = $name;
                $row->email = $email;
                $dateObject = new Zend_Date($date);
                $row->date = $dateObject->get(Zend_Date::TIMESTAMP);
                $row->url = $url;
                $row->description = $description;
                $row->priority = $priority;
                
                // save the new row
                $row->save();
                // now fetch the id of the row you just created and return it
                $id = $this->_db->lastInsertId();
                print $id; exit;

        }
}

THE SQL for creating table:

Code:

CREATE TABLE `bugs` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`author` varchar(250) DEFAULT NULL,
`email` varchar(250) DEFAULT NULL,
`date` int(11) DEFAULT NULL,
`url` varchar(250) DEFAULT NULL,
`description` text,
`priority` varchar(50) DEFAULT NULL,
`status` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
)

Found this:
[#ZF-8649] lastInsertId for PDO_MYSQL adapter returns "0" - Zend Framework
Issue Tracker

Maybe addresses the same issue, and seems it is a bug.


I've tried with 1.10, the problem stills there.
I really need to fix this, because I can't go further with my Zend Framework
learning.

-- 
View this message in context: 
http://n4.nabble.com/insert-or-save-Zend-Db-Table-tp1575952p1575952.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to