Reputation: 54949
I have two models
Rest (id, name .....)
Operating_hours(id, open, close, rest_id)
When I try to save a record from Restaurant add form. It saves only the open and the close time but not the reference id at rest_id.
$this->Restaurant->create();
if($this->Restaurant->saveAll($this->data, array('validate' => 'first'))) {
$this->Session->setFlash(__('The restaurant has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The restaurant could not be saved. Please, try again.', true));
}
Upvotes: 2
Views: 1627
Reputation: 4398
If you are doing an insert (since it is an "add"), it is unlikely that you can perform everything in one step, as MySQL would not know the id of your restaurant to save the opening hours. I'd suggest doing the following:
$this->Restaurant->create();
if($this->Restaurant->save($this->data, array('validate' => 'first'))) {
$this->data['rest_id'] = $this->Restaurant->getLastInsertId();
if($this->Restaurant->OperatingHours->save($this->data, array('validate' => 'first'))) {
$this->Session->setFlash(__('The restaurant has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The restaurant opening hours could not be saved. Please, try again.', true));
}
} else {
$this->Session->setFlash(__('The restaurant could not be saved. Please, try again.', true));
}
Upvotes: 2