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?
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?– Daniel Costa
I made some modifications and the error has changed, now and returns that the
$event
isUndefined variable
– Daniel Costa
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– Jeferson Assis
I think it is the way, but now he registered the campaign, redirected but not yet added Job
– Daniel Costa
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
– Jeferson Assis
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?– Daniel Costa
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!
– Daniel Costa