insert_id in Kohana 3

霸气de小男生 提交于 2019-12-12 23:04:58

问题


I'm using Kohana 3 framework with Mysql stored procedures. How can I get id of the last inserted record? Here's the code:

class Model_MyModel extends Kohana_Model
{
    public function insertNew($param1, $param2)
    {
        $result = $this->_db->query(Database::INSERT, 'CALL insertNew('.$param1.', '.$param2.', false)';
        return $result;
    }
    ...
    ...
}

Documentation says, the query() method returns an array with the last insert id and affected rows number, when executing an insert query. When I call: print_r($result) I'm getting: Array ( [0] => 0 [1] => 1 ) The insert_id key is 0, though I'm having many records in the db. What I'm doing wrong?


回答1:


I think you'll have to use SQL's LAST_INSERT_ID() after inserting using a procedure:

SELECT LAST_INSERT_ID() as last_insert_id FROM table_name

( in your procedure just define this query in the end ).

The problem in this case is that Kohana automatically returns mysql_insert_id and mysql_affected_rows as result for Database::INSERT, so you'll need to call the procedure as a SELECT query and fetch it (Database::SELECT).



来源:https://stackoverflow.com/questions/4250621/insert-id-in-kohana-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!