[英]Save complete model in session - Codeigniter
I want to save complete model in session, so I can use data easily in entire application. 我想在会话中保存完整的模型,以便可以在整个应用程序中轻松使用数据。
After successful login in application I am saving model in session and session save in database in ci_session
table. 成功登录应用程序后,我将模型保存在会话中,并将会话保存在
ci_session
表的数据库中。
Code I tried: 我试过的代码:
$loginSuccess = $this->login_model->doLogin($username, $password);
if($loginSuccess) {
$this->login_model->initialize(); // this will set value in private variable
$serializelogin = serialize($this->login_model);
$this->session->set_userdata('userprofile', $serializelogin);
}
This code gives me an error: 这段代码给我一个错误:
Error Number: 2006
MySQL server has gone away
Update: I change user_data
column of table ci_session
from text to longtext 更新:我将表
ci_session
user_data
列从文本更改为ci_session
U need to start by creating a session. 您需要先创建一个会话。
//Destroy old session
$this->session->sess_destroy();
//Create a fresh, brand new session
$this->session->sess_create();
//Set session data
$this->session->set_userdata($row);
Basically $row is an array. 基本上$ row是一个数组。 $row = $serializelogin->row_array();
$ row = $ serializelogin-> row_array();
or 要么
$this->session->set_userdata(array('active_flag' => false, 'active_status' => 0));
make sure the data which has been returned from the model is in row_array format. 确保从模型返回的数据为row_array格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.