Condition inside foreach before saving model

Asked

Viewed 175 times

1

In my Controller of Cakephp was made a foreach passing all data from a form to a variable.

Now, how do I make one if specific, that its objective would be if the value that the foreach is passing is empty, pass to the next?

foreach ($this->request->data['CursoNavigation'] as $indice => $valor) {
    if (empty($valor)) continue;
    // código para continuar e salvar as informações no 'CursoNavigation'
}
  • This code seems to work. Some problem has occurred?

  • 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 ?

  • 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?

  • 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.

1 answer

1

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)) {
}
  • I still need to get an id, from another model, in my table there is a column called curso_id that saves that value. So my code looks like this, but it gives an error when saving, I did something wrong? $this->Curso->create();
 foreach($this->request->data['CursoNavigation'] as $valor ){
 if(empty($valor)) continue;
 $valor['curso_id'] = $this->Curso->id;
 $this->CursoNavigation->create();
 $this->CursoNavigation->save($valor);
 }

  • It is always good for you to give us a full example, so it becomes clearer your doubt. You can edit your question and add all this information. With this comment of yours, I already started not to understand exactly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.