If you do not want a particular field of your form not to be saved in the database, even if it is a string empty, you can treat this behavior on own Model
.
In his Model
Cursonavigation just you implement a callback called beforeSave()
, that might be something like this:
public function beforeSave($options = array()) {
if (empty($this->data[$this->alias]['campoX'])) {
unset($this->data[$this->alias]['campoX']);
}
return true;
}
That is, if at the time the data from this Model
is saved, the field campoX
is empty, then it will be removed from your array remaining a null value in the database.
And the method to save in your Controller
remains the same:
if ($this->CursoNavigation->save($this->request->data)) {
}
That goes for any time that you save this Model
, but if only for a action specific, you will indeed need to do this directly on Controller
:
$data = $this->request->data['CursoNavigation'];
foreach ($data as &$valor) {
if (empty($valor))
unset($valor);
}
if ($this->CursoNavigation->save($data)) {
}
This code seems to work. Some problem has occurred?
– Paulo Rodrigues
I wanted to understand one thing, every time the foreach runs, this $value variable, is with a different array? If this code is right, as I do now, to save the data ?
– Math's
For that I need to know what’s inside
$this->request->data['CursoNavigation']
, which is the representation of the data in your form. And besides, what exactly do you want that is not empty to not proceed? Some field of the specific form?– Paulo Rodrigues
I have a form that contains 'N' information, that information, not all of it should be filled in, and I want my controller to do the following, every time he goes through the array that contains the information, he checks whether it is empty or not, if he is, he "jumps" this information, reaching the end of this loop, it saves all information.
– Math's