0

client.id用下面的语法写了我的会话。

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

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

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

它正在打印数组id

现在Controller我尝试了下面的代码来获取 session 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
                )

        )

)
4

2 回答 2

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"]);
    }
}

并检查您的条件是否设置了 Session 是这样的

if($this->Session->check("Client.id")){
    $client_id = $this->Session->read("Client.id");
}else{
   //handle if session is not set
}
于 2015-11-03T06:57:46.607 回答
0

我解决了这个问题只需添加以下代码

$this->Session->write('Client', $client_id[0]["Client"]['id']);
于 2015-11-03T08:33:01.657 回答