Question

I am working on a CakePHP 2.x. The scenario is I am sending an encrypted and decrypted data to the database. So in order to do this I have written beforeSave function in each modal. so right now the problem is whenever data is updated, the data is not going encrypted into db .. please anyone know how to i fix this issue

I am doing this in my controller. The update and save function:

    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 in Model

      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 function

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

please help me if anyone know how to deal with this issue.

Was it helpful?

Solution 2

here better to use save function for updating data like:

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

where $data contains all field that you want to update with value

OTHER TIPS

Try following code in model

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

look here http://nuts-and-bolts-of-cakephp.com/2010/01/27/make-updateall-fire-behavior-callbacks/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top