我正在开发CakePHP 2.x.方案是我将加密和解密的数据发送到数据库。所以为了做到这一点,我在每个模态中都编写了beforeSave
函数。
所以现在问题是每当数据更新时,数据都不会加密到数据库中..请任何人都知道如何解决这个问题
我在我的控制器中这样做。更新和保存功能:
foreach($data as $datas){
$count = $this->Contact->checkkey($datas['idUser'],$datas['key']);
if($count>0){
$this->Contact->updateContactAgainstkey($datas['name'],
$this->request->data['Contact']['mobileNo'],
$this->request->data['Contact']['other'],
$this->request->data['Contact']['email'],
$datas['key'],$datas['idUser']);
}else{
$this->Contact->create();
$this->Contact->save($this->request->data);
}
}
模型
中的updateFunction public function updateContactAgainstkey($name,$mobileNo,
$other,$email,$key,$userid){
if($this->updateAll(
array('name' => "'$name'",
'mobileNo' => "'$mobileNo'",
'workNo' => "'$workNo'",
'homeNo' => "'$homeNo'",
'other' => "'$other'",
'email' => "'$email'",),
array('User_id'=>$userid,'key'=>$key))){
return true;
}else{
return false;
}
}
beforeSave功能
public function beforeSave($options=array()) {
if ( isset ( $this -> data [ $this -> alias ] [ 'mobileNo' ] ) ) {
$this -> data [ $this -> alias ] [ 'mobileNo' ] = AllSecure::encrypt($this->data[$this->alias]['email']);
}
return true;
}
如果有人知道如何处理这个问题,请帮助我。
答案 0 :(得分:2)
在模型中尝试以下代码
public function updateAll($fields, $conditions = true) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$created = FALSE;
$options = array();
if($db->update($this, $fields, null, $conditions)) {
$created = TRUE;
$this->Behaviors->trigger($this, 'afterSave', array($created, $options));
$this->afterSave($created);
$this->_clearCache();
$this->id = false;
return true;
}
return FALSE;
}
看这里 http://nuts-and-bolts-of-cakephp.com/2010/01/27/make-updateall-fire-behavior-callbacks/
答案 1 :(得分:1)
这里最好使用保存功能来更新数据,如:
$data=array();
$data['Contact']['mobileNo']=$this->request->data['Contact']['mobileNo'];
$data['Contact']['other']=$this->request->data['Contact']['other'];
$data['Contact']['other']=$this->request->data['Contact']['other'];
........... .............. ................
$this->Contact->id = "primerykey";
$this->Contact->save($data);
其中$ data包含您要使用值
更新的所有字段