0

我正在开发 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);
          }
     }

模型中的更新函数

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

之前保存功能

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

如果有人知道如何处理这个问题,请帮助我。

4

2 回答 2

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/

于 2013-08-14T12:12:39.677 回答
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 包含您要使用值更新的所有字段

于 2013-08-14T10:13:35.047 回答