2010-09-30 25 views
2

我有兩個型號CakePHP的節約等模型數據

休息(ID,姓名.....)
Operating_hours(ID,打開,關閉,rest_id)

當我嘗試保存記錄從餐廳添加表單。它只保存打開和關閉時間,但不保存rest_id中的引用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)); 
} 
+0

你可以發佈兩個模型文件?問題可能在那裏。 – Adam 2010-09-30 09:34:41

+0

您是否在餐廳模特中擁有適當的關係?即餐廳有許多營業時間和營業時間屬於餐廳?你爲什麼不把rest_id重命名爲restaurant_id? – 2010-09-30 09:53:42

+0

@Nik按照慣例。抱歉。同時打字我做了簡短的表格。我發現了錯誤。 restaurant_id – 2010-09-30 15:55:58

回答

2

如果你正在做一個插件(因爲它是一個「添加」),這是不可能的,你可以一步執行的一切,像MySQL不會知道你的餐廳的ID保存的開放時間。我建議做以下幾點:

$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)); 
} 
+0

的烘焙代碼中有一條驗證規則,謝謝。模型中的rest_id有一個驗證規則,它不會保存它。 :D在刪除它後立即運行:D – 2010-09-30 15:57:05