2012-01-26 93 views
0

在cakephp 2中,我有一個表單爲公司和用戶表創建一個新記錄。cakephp沒有哈希模型中的密碼

我的問題是,它保存到這兩個表,但在Umuser表中它不散列密碼或ID密鑰。它似乎根本不稱任何Umuser模型。這是一個保存之前,它不會這樣。

public function beforeSave() { 
    if (isset($this->data[$this->alias]['password'])) { 
     $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    } 
    return true; 
} 

Umuser處於puging/Usermin/Models目錄中。

公司控制器來保存數據:

public function add() { 
    if ($this->request->is('post')) { 
     $this->Company->create(); 

     if ($this->Company->saveAll($this->request->data, array('validate'=>'first'))) { // Should ensure both sets of model data get validated 
      $this->Session->setFlash(__('The company has been saved')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The company could not be saved. Please, try again.')); 
     } 
    } 
} 

看不出爲什麼它保存數據,但繞過Umuser模型來做到這一點。

+0

關聯這兩個型號? – entropid

+0

我的提示:把這些東西放在一個集中的位置,以保持代碼乾燥。一種行爲是在模型層面上做到這一點的一種好方法:http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/ – mark

+0

是的模型是相關的。 umuser表有一個名爲company_id的字段。它確實保存了數據,而不是在beforesave中保存散列 –

回答

1

$ this-> data [$ this-> alias] ['password'])這裏可能有些錯誤。 嘗試做這樣的事情:

public function beforeSave() { 
if (isset($this->data[$this->alias]['password'])) { 
    $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    return true; 
}else{ 
    return false; // if $this->data[$this->alias]['password'] is not set, don't save the model 
} 

}

然後張貼在這裏,如果該公司已經保存