CakePHP 2.6:控制器中的会话读取不起作用

时间:2015-11-03 05:35:24

标签: cakephp cakephp-2.0 cakephp-2.6

我已通过以下语法在会话中写了client.id

$this->Session->write('Client.id', $client_id);

然后我在view.ctp中检查了它,如下面的代码

<pre><?php print_r($this->Session->read('Client.id')); ?> </pre>

它的id打印数组。

现在在Controller我尝试了以下代码来获取会话client.id

$client_id = $this->Session->read('Client.id');

在这段时间我没有得到$client_id。有没有错。愿有人帮帮我吗?

我在评论

中添加了更多代码

在用户控制器中,我已在登录操作中设置了用户$client_id

if($user_role == 'client')
    {
           $user_email = $this->Auth->user('email');
           $this->loadModel('Client');
           $client_id  = $this->Client->find('all',
                                              array(
                                                         'conditions'=>array('Client.email'=>$user_email),
                                                         'fields' => array('Client.id'),
                                                         'limit'  => 1,
                                                         'recursive' => -1, //int
                                          )
         );

         $this->Session->write('Client.id', $client_id);
 }

这里的代码我试图在条件

中应用它
if($role == 'client'){
           $this->AppComment->recursive = 0;

           $client_id = $this->Session->read('Client.id');


           $this->Paginator->settings = array(
                    'conditions' => array('AppComment.client_id' => $client_id)
           );

           $this->set('appComments', $this->Paginator->paginate());
}

我发现了错误

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'

这是

的输出
<pre><?php print_r($this->Session->read('Client.id')); ?> </pre>

我得到的输出

  Array
(
    [0] => Array
        (
            [Client] => Array
                (
                    [id] => 1
                )

        )

)

2 个答案:

答案 0 :(得分:1)

在登录操作中尝试此操作

 if($user_role == 'client'){
     $user_email = $this->Auth->user('email');
     $this->loadModel('Client');
     $client_id  = $this->Client->find('first',
                       array(
                            'conditions'=>array('Client.email'=>$user_email),
                            'fields' => array('Client.id'),
                            'recursive' => -1, //int
               )
    );
    if(!empty($client_id)){
       $this->Session->write('Client.id', $client_id["Client"]["id"]);
    }
}

如果会话设置与否,请检查您的条件

if($this->Session->check("Client.id")){
    $client_id = $this->Session->read("Client.id");
}else{
   //handle if session is not set
}

答案 1 :(得分:0)

我解决了这个问题,只需添加下面的代码

$this->Session->write('Client', $client_id[0]["Client"]['id']);