2
votes

My form contains a model object that contains five child objects that are related using hasMany. When I save the form, I notice that all fields, regardless if they are empty, are saved into the database. Is it possible to set a condition in the beforeSave() callback method to prevent child items that have no values from being saved? I have tried to unset the key in the array containing empty values but the row is still being added into the database.

Here is the code for my 'Mtd' model. The Mtd model contains many Flowratereatments. On my form, I have a checkbox that says 'This is a Flow Rate Based Treatment'. So, if a user clicks on that, then the user can fill it in the fields. However, if the user does not fill it in, I want to prevent a new row from being added with just the foreign key of the Mtd table.

<?php
class Mtd extends AppModel {
   public $name = 'Mtd';
   public $hasOne = array('Treatmentdesign', 'Volumetreatment');
   public $hasMany = 'Flowratetreatment';


   function beforeSave() {
      if($this->data['Mtd']['is_settling'] != 1){
         unset($this->data['Flowratetreatment'][0]);
      } 
      return true;
   }
}

?>
2

2 Answers

0
votes

Did you tried something like:

class User extends AppModel {
    function validates() {
        $this->setAction();
        #always validate presence of username
        $this->validates_presence_of('username');
        #validate uniqueness of username when creating a new user
        $this->validates_uniqueness_of('username',array('on'=>'create'));
        #validate length of username (minimum)
        $this->validates_length_of('username',array('min'=>3));
        #validate length of username (maximum)
        $this->validates_length_of('username',array('max'=>50));
        #validate presence of password
        $this->validates_presence_of('password');
        #validate presence of email
        $this->validates_presence_of('email');
        #validate uniqueness of email when creating a new user
        $this->validates_uniqueness_of('email',array('on'=>'create'));
        #validate format of email
        $this->validates_format_of('email',VALID_EMAIL);

        #if there were errors, return false
        $errors = $this->invalidFields();
        return (count($errors) == 0);
    }
}
?> 

in your model

0
votes

I have used this code:

public function beforeSave() {
    if(isset($this->data[$this->alias]['profile_picture'])) {
        if($this->data[$this->alias]['profile_picture']['error']==4) {
            unset($this->data[$this->alias]['profile_picture']);
        }
    }
    return true;
}

in a previous app, to remove a key from $this->data if the user had not uploaded a file, to prevent the old value being overwritten.

this should work for you (you'll need to adapt it; based on what $this->data contains at this point.

public function beforeSave() {
    if(empty($this->data[$this->alias]['the_key'])) {
        unset($this->data[$this->alias]['the_key']);
    }
    //debug($this->data); exit; // this is what will be saved
    return true;    
}

you mention you tried this already? post your code in your original post.