Hello!
First of all I’m glad there is a forum where I can ask questions since I’ve been desperately searching for answers but can’t quite get it right.
I’m building a webapp with cakephp and one of my add functions should insert data into one table while adding data into another linked to the original insertion.
It’s for vehicle checklists, it adds one row on a table with the current user_id, vehicle_id and date while adding onto another table all of the items_id with a 1 or 0 on a checked field. (pictures to illustrate)
It’s not inserting the multiple lines on the second table
I’ve tried SaveMany-> as well, that gives an error.
public function add()
{
$checklist = $this->Checklists->newEmptyEntity();
$checkrelatorios = $this->Checklists->Checkrelatorios->newEmptyEntity();
debug($this->request->getData());
if ($this->request->getData('veiculo_id') && $this->request->getData('user_id') == null){
$items = $this->Checklists->Checkrelatorios->Items->find('all', array('conditions' => array('Items.veiculo_id' => $this->request->getData('veiculo_id')), 'order' => array('Items.compartment_id' => 'ASC')));//debug($items);die;
$compartments = $this->Checklists->Checkrelatorios->Items->Compartments->find('list');
$comps = $compartments->toArray();
$this->set(compact('comps', 'compartments', 'items'));
}else if ($this->request->getData('user_id')){//debug($checklist);die;
if ($this->request->is('post')) {//debug($this->request->getData());
$checklist = $this->Checklists->patchEntity($checklist, $this->request->getData());
//$checkrelatorios = $this->Checklists->Checkrelatorios->newEntities($this->request->getData('checkrelatorio'));
//debug($checklist);die;
if ($this->Checklists->Checkrelatorios->Save($checklist)) {//debug($checklist);die;
//$this->Checklists->Checkrelatorios->SaveMany($checkrelatorios);
$this->Flash->success(__('The checklist has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The checklist could not be saved. Please, try again.'));debug($checklist);
}
}
$user = $this->Auth->user('id');
$veiculos = $this->Checklists->Veiculos->find('list', ['limit' => 200]);
$veicEsc = $veiculos->toArray();
$this->set(compact('checklist', 'veicEsc', 'user', 'veiculos'));
}