tags:

views:

25

answers:

1

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 open and 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));
        }
+1  A: 

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));
}
Damien
thanks. there was a validation rule with the rest_id in the model which was not letting it save. :D works now after removing it :D
Harsha M V