简体   繁体   English

如何获取最后插入的ID,然后将其分配给全局变量

[英]How to get last inserted id, then assign it to a global variable

I want to get last inserted id, and then assign it to a global variable to use it later. 我想获取最后插入的ID,然后将其分配给全局变量以供以后使用。

I'm use callback_after_insert as the following: 我使用callback_after_insert如下:

$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));

function _callback_get_lastInsertID($post_array,$primary_key) {
   $this->lastInsertedId = $primary_key;
}

Then, when use the $this->lastInsertedId to get its value, but I do not find any value ​​stored. 然后,当使用$this->lastInsertedId来获取其值时,但我找不到存储的任何值。

function featured_ad_offer() { 
    echo $this->lastInsertedId; 
    @$data['content'] .= $this->load->view('featured_ad_offer', '', true); 
    $this->load->view('index', $data); 
}

There is an error message say Undefined property: Content::$lastInsertedId 有一条错误消息,指出Undefined property: Content::$lastInsertedId

Now, how to do that ? 现在,该怎么做?

Please try declaring your variable in class (assuming both functions are in the same class) right after "declaring" class itself ie 请尝试在“声明”类本身之后立即在类中声明变量(假设两个函数都在同一类中),即

class Blog extends CI_Controller {
    private $lastInsertedId = NULL;

    public function fcn1() {$this->lastInsertedId = 3.14;}
    public function fcn2() {var_dump($this->lastIndertedId);}

}

If you really mean using it as global variable you need to declare it in CI_Controller , or better yet create file in /core/Public_Controller that extends CI_Controller and let all your controllers be extended not by CI_Controller but Public_Controller therefore you can easily declare variables that are "global". 如果你真的是用它作为global variable需要声明它CI_Controller ,或者更好的创建文件/core/Public_Controller延伸CI_Controller ,并让所有的控制器可以通过不延长CI_ControllerPublic_Controller因此,你可以很容易地声明是变量“全球”。

For more information on extending use this tutorial by Phil . 有关扩展的更多信息,请使用Phil的教程

If you have troubles here is a user guide link . 如果你有麻烦在这里是一个用户指南链接

There is another option (not recommended!) config class , 还有另一个选项(不推荐!) config class

$this->config->set_item('item_name', 'item_value'); //setting a config value
$this->config->item('item name'); //accesing config value

maybe you can use this to get the last inserted id 也许您可以使用它来获取最后插入的ID

$this->db->insert_id();

store it in the session 将其存储在会话中

$this->load->library('session');
$this>session->set_userdata('last_inserted_id');

and use this session variable in other places 并在其他地方使用此会话变量

$last_inserted_id = $this->session->userdata('last_inserted_id');

wish this help :D 希望这个帮助:D

The solution that you need it can easily be resolved with Codeigniter sessions. 您需要的解决方案可以通过Codeigniter会话轻松解决。 So for example, what you can do is: 因此,例如,您可以做的是:

$crud->callback_after_insert(array($this, '_callback_get_lastInsertID'));

function _callback_get_lastInsertID($post_array,$primary_key) {
   $this->session->set_userdata('last_insert_id', $primary_key);
}

Make sure that you have session class loaded first. 确保首先加载了会话类 The best way to do it is to actually have the sessions library at the autoload. 最好的方法是自动加载会话库。

Now to actually call the last_insert_id again, you can simply do it by calling: 现在要再次实际调用last_insert_id,您只需通过调用即可:

echo $this->session->userdata('last_insert_id');

So in your example you can simple do: 因此,在您的示例中,您可以简单地执行以下操作:

function featured_ad_offer() { 
    echo $this->session->userdata('last_insert_id'); 
    @$data['content'] .= $this->load->view('featured_ad_offer', '', true); 
    $this->load->view('index', $data); 

} }

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

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