2016-01-21 57 views
1

我正在使用Phalcon PHP,並且我想在創建會話後向會話中添加其他項目。我有這樣的:如何在創建會話後編輯會話

private function _registerSession($user, $account) { 
    $this->session->set('auth', array(
     'user_id' => $user->id, 
     'username' => $user->name 
    )); } 

在另一個控制器我想編輯這個會話例如:

$auth = $this->session->get('auth'); 
$auth->add('account_id', '10'); 

而本屆會議將內容3個變量爲:

$this->session->set('auth', array(
     'user_id' => $user->id, 
     'username' => $user->name, 
     'account_id' => 10 
    )); } 

但我不我不知道該怎麼做。

+1

這不起作用:PHP致命錯誤:調用一個成員函數集()非對象 – John

+1

'$ AUTH = $這 - >會話而上>得到( 'AUTH'); $ auth ['account_id'] ='10'; $ this-> session-> set('auth',$ auth);' –

+0

完美!非常感謝:) – John

回答

3

呦需要做的是在下列方式: -

$auth = $this->session->get('auth'); // get auth array from Session 
$auth['account_id']= '10'; // add new index value pair to it 
$this->session->set('auth',$auth); // reassign it to auth index of Session 
+0

謝謝你的男人;) – John

+0

歡迎@ John.Thanks –

0
private function _registerSession($user, $account) { 
    $this->session->set_userdata('auth', array(
     'user_id' => $user->id, 
     'username' => $user->name 
    )); } 

// You should use the following code to set one more parameter in sesssion: 

    $this->session->set_userdata('auth', array(
     'user_id' => $this->session_userdata('user_id'), 
     'username' => $this->session_userdata('username'), 
     'account_id' => 10 
    )); 
1

這應該工作:

$auth = $this->session->get("auth"); 
$this->session->set("auth", array_merge($auth, array('account_id'=>'10'))); 
1

我認爲你可以使用這樣的: -

$auth = $this->session->get('auth'); 
$auth['account_id']= 10; 
$this->session->set('auth',$auth); 
0

Phalcon的會話代碼只是0123的一個包裝。最簡單的解決方法是避免使用多爾康功能:

$_SESSION['auth']->add('account_id',10);