Codeigniter 3 应用程序错误:无法在会话中存储某些列值

Codeigniter 3 application bug: unable to store certain column value in a session

我正在使用 Codeigniter 3 开发 社交网络 应用程序,Ion-Auth and Bootstrap 4. You can see the Github repo HERE.

我想将登录用户的first_name存储到当前会话中。为此,我在原来的 $session_data 数组中添加了行 'user_first_name' => $user->first_name,

$session_data = [
        'identity'                 => $user->{$this->identity_column},
         $this->identity_column     => $user->{$this->identity_column},
        'email'                    => $user->email,
        'user_id'                  => $user->id,
        'user_first_name'          => $user->first_name, //retrieve from the first_name column
        'old_last_login'           => $user->last_login,
        'last_check'               => time(),
        'ion_auth_session_hash'    => $this->config->item('session_hash', 'ion_auth'),
];

重点是在用户 ID 登录后显示欢迎消息:

Welcome, <?php echo $this->session->userdata('user_first_name'); ?>

我收到 Undefined property $first_name 消息,欢迎消息只包含“欢迎”,(尽管 $this->session->userdata('user_first_name') 确实 return 用户的id).

少了什么?

快速检查 Ion_auth_model.php 中的 $userset_session() 是什么使用 var_dump 显示为...

LINE: 1944 Module Ion_auth_model
object(stdClass)#26 (5) {
  ["email"]=>
  string(21) "frednurk@gmail.com"
  ["id"]=>
  string(1) "8"
  ["password"]=>
  string(60) "y$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"
  ["active"]=>
  string(1) "1"
  ["last_login"]=>
  string(10) "1599322333"
}

其中不包含first_name。由于您的错误消息,这正是您所期望的。

搜索此调用的位置,将带您返回 Ion_auth_model.php - 登录,其中 $user 的 select 查询是

$query = $this->db->select($this->identity_column . ', email, id, password, active, last_login')
                  ->where($this->identity_column, $identity)
                  ->limit(1)
                  ->order_by('id', 'desc')
                  ->get($this->tables['users']);

正如预期的那样,它丢失了 first_name

所以将 first_name 添加到 Select 中...

$query = $this->db->select($this->identity_column . ', email, id, password, active, last_login, first_name')
                  ->where($this->identity_column, $identity)
                  ->limit(1)
                  ->order_by('id', 'desc')
                  ->get($this->tables['users']);

导致 $user 成为...

LINE: 1944 Module Ion_auth_model
object(stdClass)#26 (6) {
  ["email"]=>
  string(21) "frednurk@gmail.com"
  ["id"]=>
  string(1) "8"
  ["password"]=>
  string(60) "y$MGlESdMfrr/UNd.mdW4Vr.nyTqES0SB6rEnaDBOYocwU7yLXVqNMq"
  ["active"]=>
  string(1) "1"
  ["last_login"]=>
  string(10) "1599322333"
  ["first_name"]=>
  string(3) "Tim"
}

然后就很开心了。

结论

  1. 阅读错误消息
  2. 通过检查您的 variables/array/objects 来执行调试以查看它们是什么。
  3. 回过头来修复它。

调试时,我使用了这个小代码片段

echo '<pre>';
echo 'LINE: '. __LINE__. ' Module '.__CLASS__.'<br>';
var_dump(WHAT_TO_LOOK_AT_GOES_HERE);
echo '</pre>';