2015-11-03 24 views
0

我已按照以下語法在會話中編寫了我的client.idCakePHP 2.6:會話讀取控制器不工作

$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。有沒有任何錯誤。有誰可以幫助我嗎?

在這裏,我在評論中加入更多的代碼爲提

在用戶控制器我在登錄操作

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

這裏設置用戶$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 
       ) 

     ) 

) 
+0

一切似乎是正確的,我會幫助,如果你提供你的代碼更片斷 –

+0

OK讓我增加更多code.I編輯我的問題。 –

+0

一切看起來都很好。檢查數據來了或不在$ client_id中。 – Shaddy

回答

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 
} 
+0

我得到相同的結果:(如果我使用你的代碼,那麼沒有任何價值的是在會話中設置,我已經檢查。 –

+0

檢查現在,編輯答案。我有任何記錄存在與您的條件(Client.email => $ user_email),它會工作 –

+0

我在這裏得到的錯誤異常。是有記錄。 –

0

我解決這個問題,只需添加以下代碼

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

我在我的答案中添加了這個,但是如果寫find(「first」)而不是find(「all」),那麼將不需要[0] –

+0

好吧,請添加你喜歡的答案。 –

+0

我很高興你的答案 –