Updating multiple models and a single form CakePHP 3.x

0

I'm having trouble updating more of the model in the same form in cakephp, I've done several different modes and in all the main model is inserted / updated because the related model does not.

My controller looks 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 model data "Campaigns" are inserted, but 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 have checked the variables $jobs and $campanhas both are correct and with the data coming from the form, and does not present any error and success, it only saves the campaign and does not save the job.

Any light?

    
asked by anonymous 13.09.2016 / 16:05

1 answer

1

You do not need to create different objects when you have a relationship.

link

Build relationships 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 you have any errors and return the correct message to the user, eg:

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>
    
13.09.2016 / 16:42