Updating of several Models and a single Cakephp 3.x form

Asked

Viewed 114 times

0

I’m having trouble upgrading more models in the same form in cakephp, I’ve done several different modes and in all the main model is inserted/updated but the related model is not.

My controller is like this:

    $this->loadModel('Jobs');

    $campanha = $this->Campanhas->newEntity();
    $jobs = $this->Jobs->newEntity();

    if ($this->request->is('post')) {
        $jobs = $this->Jobs->patchEntity($jobs, $this->request->data);
        $campanha = $this->Campanhas->patchEntity($campanha, $this->request->data);
        $save = $this->Campanhas->save($campanha);
        if ($save) {   

            $jobs['campanha_id'] = $save->id;

            $savejob = $this->Job->save($jobs);
            if($savejob){
                $this->Flash->success(__('The job has been saved.'));
                $this->redirect(array('action' => 'index'));
            }else{
                $this->Flash->success(__('The job hasnt been saved.'));
            }

        }else {
            $this->Flash->error(__('The job hasnt been saved.'));
        }
    }

however, as I said before, the data from the "Campaigns" model are entered, but the data from the "Jobs" model is not.

The form looks like this:
<fieldset> <legend><?= __('Add Campanha') ?></legend> <?php echo $this->Form->input('Campanhas.nome'); echo $this->Form->input('Campanhas.data', ['class' => 'datepicker']); echo $this->Form->input('Campanhas.cliente_id', ['options' => $cadastros, 'id' => 'cliente_id']); echo $this->Form->input('Campanhas.contato_id', ['options' => $contatos, 'id' => 'contato_id']); echo $this->Form->input('Campanhas.requisitante_id', ['options' => $users]); echo $this->Form->input('Campanhas.briefing'); ?> </fieldset> <fieldset> <legend><?= __('Add Job') ?></legend> <?php echo $this->Form->input('Jobs.nome'); echo $this->Form->input('Jobs.status_id', ['options' => $status]); echo $this->Form->input('Jobs.prioridade_id', ['options' => $prioridades]); echo $this->Form->input('Jobs.iniciar'); echo $this->Form->input('Jobs.concluir'); echo $this->Form->input('Jobs.estimado'); echo $this->Form->input('Jobs.gasto'); echo $this->Form->hidden('Jobs.campanha_id'); ?> </fieldset>

I’ve checked the variables $jobs and $campanhas both are correct and with the data coming from the form, and does not present any error or success, just saves the campaign and does not save the job.

Some light?

1 answer

1


You don’t have to create different objects when you have a relationship.

http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-Associations

Creates the relationship in models

//Model Campanhas
$this->hasOne('Jobs');

//Model Jobs
$this->belongsTo('Campanhas');

Specify in the creation of the campaign object that you will save the relationship

$this->Campanhas->patchEntity($campanha, $this->request->data, [
    'associated' => ['Jobs']
]);

I advise you to always check if there are any errors and return the correct message to the user, Example:

if ($this->request->is('post')) {
    $campanha = $this->Campanhas->patchEntity($campanha, $this->request->data, [
        'associated' => ['Jobs']
    ]);

    if($errors = $campanha->errors()) {
        $this->Flash->error('Ocorreu o(s) seguinte(s) error(s):', [
            'params'=>$errors
            ]);
    }
    else if ($this->Campanhas->save($campanha)) {
        $this->Flash->success('The job has been saved.');

        $this->redirect([
            'action'=>'add',
            $campanha->id
            ]);
    }
}

Change your form to

<fieldset>
    <legend><?= __('Add Campanha') ?></legend>
    <?php
        echo $this->Form->input('nome');
        echo $this->Form->input('data', ['class' => 'datepicker']);
        echo $this->Form->input('cliente_id', ['options' => $cadastros, 'id' => 'cliente_id']);
        echo $this->Form->input('contato_id', ['options' => $contatos, 'id' => 'contato_id']);
        echo $this->Form->input('requisitante_id', ['options' => $users]);
        echo $this->Form->input('briefing');
    ?>
</fieldset>
<fieldset>
    <legend><?= __('Add Job') ?></legend>
    <?php
        echo $this->Form->input('job.nome');
        echo $this->Form->input('job.status_id', ['options' => $status]);
        echo $this->Form->input('job.prioridade_id', ['options' => $prioridades]);
        echo $this->Form->input('job.iniciar');
        echo $this->Form->input('job.concluir');
        echo $this->Form->input('job.estimado');
        echo $this->Form->input('job.gasto');
        echo $this->Form->hidden('job.campanha_id');
    ?>
</fieldset>
  • I believe I was mixing information from several old versions and trying to make it work. Relationships are right and all, and running the way you recommended me he returns Ocorreu o(s) seguinte(s) error(s): without the list of errors that occurred. I need to initialize the $errors somehow?

  • I made some modifications and the error has changed, now and returns that the $event is Undefined variable

  • I made a correction, change the $event->id for $campanha->id, as I coped from a code that had, I forgot to change this variable, probably showed the errors without displaying the list, because its model is not validating anything, but it was not possible to save

  • I think it is the way, but now he registered the campaign, redirected but not yet added Job

  • Try to leave the data you are sending to form, similar to this format http://book.cakephp.org/3.0/en/orm/saving-data.html#saving-hasone-Associations, I will edit your form and put in the answer

  • Really the problem is in the relationship, I put the Form as Voce suggested and the relationship as hasOne and he saved, however, the campaign must have more than one job. I do not know if my logic is wrong, but even with hasOne in the future this same campaign may have more Joes?

  • Dude, I tested it here and it worked, I added another job to the same campaign and it was all OK, thanks for your help!

Show 2 more comments

Browser other questions tagged

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