【发布时间】:2014-04-11 21:45:53
【问题描述】:
我想获取最后插入的 id,然后将其分配给全局变量以供以后使用。
我使用callback_after_insert如下:
$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));
function _callback_get_lastInsertID($post_array,$primary_key) {
$this->lastInsertedId = $primary_key;
}
然后,当使用$this->lastInsertedId 获取它的值时,我没有找到任何存储的值。
function featured_ad_offer() {
echo $this->lastInsertedId;
@$data['content'] .= $this->load->view('featured_ad_offer', '', true);
$this->load->view('index', $data);
}
有一条错误消息说Undefined property: Content::$lastInsertedId
现在,该怎么做?
【问题讨论】:
-
有关您使用 $this 的更多信息会有所帮助。你在同一个对象中吗?你确定它是一开始就设置好的吗?
-
Are you in the same object?-> 是的。Are you sure it gets set in the first place ?-> 是的。 -
如果它被设置,那么它在什么时候变得未设置?函数退出后立即?
-
对不起,我不明白,但我在同一个对象的另一个方法中使用了这个变量。
public function featured_ad_offer() { echo $this->lastInsertedId ; @$data['content'] .= $this->load->view('featured_ad_offer', '', true); $this->load->view('index', $data); } -
您应该更新您的 OP,而不是将这么大的代码块复制到评论中。如果您的变量在某一时刻可用,但后来不可用,则说明正在重新设置该变量,或者您访问了错误的对象。
标签: php codeigniter grocery-crud