简体   繁体   English

Zend框架:lastInsertId返回0

[英]Zend Framework: lastInsertId is returning 0

I am currently learning Zend Framework from a book called "Appress Pro Zend Framework Techniques, Build a Full CMS Project" and I am stuck at a point where after submitting a bug, the page was suppose to redirect to confirm action, but this redirection depends on the result thrown by the Model, which saves the bug to the database. 我目前正在从一本名为《 Appress Pro Zend Framework Techniques,建立一个完整的CMS项目》的书中学习Zend Framework,但是我陷入了提交错误后要重定向该页面以confirm操作的问题,但是这种重定向取决于根据模型抛出的结果,将错误保存到数据库。

Here is the code of model bug 这是模型错误的代码

public function createBug($name, $email, $date, $url, $description, $priority, $status) {
    // create a new rows in the bugs table
    $row = $this->createRow();

    // set the row data
    $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;
    $row->status = $status;

    //Save the new row
    $row->save();

    // now fetch the id of the row you just created and return it
    $id = $this->_db->lastInsertId();
    return $id;
}

The records are saved in the database, however the $id is always returning 0, which is causing the redirection to be escaped. 记录保存在数据库中,但是$ id始终返回0,这导致重定向被转义。

Try setting $id to $row->id instead of lastInsertId() . 尝试将$id设置$id $row->id lastInsertId() $row->id而不是lastInsertId()

Most ORM's work along these lines. 大多数ORM都遵循这些原则。

 $id = $row->id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM