2013-03-17 93 views
2

我有一個表profiles和一個表users,profilesusersbelongsTo關係。在我的profiles/edit視圖中,我有一個現有users的下拉列表供您選擇。CakePHP foreignKey未保存在belongsTo關係

但是,users.id未被保存在profiles.user_id中,正如我所預料的那樣。

ProfilesController.php - >編輯:

$this->set('users', $this->Profile->User->find('list')); 

而且在查看 - >概況 - > edit.ctp

echo $this->Form->input('User'); 

控制器運行debug($this->request);表明,正確的價值觀正在被送回到控制器。保存操作是這樣的:

if ($this->request->is('post') || $this->request->is('put')) { 
    if ($this->Profile->save($this->request->data)) { 
     $this->Session->setFlash(__('The profile has been saved')); 
    } else { 
     $this->Session->setFlash(__('The profile could not be saved. Please, try again.')); 
    } 
} 

在返回的數據:

data => array(
    'Profile' => array(
     'User' => '3', 
        ... 
+0

在您的代碼的這部分沒有看到問題。如果啓用了調試,您是否檢查過SQL語句? – thaJeztah 2013-03-17 21:58:12

+0

@thaJeztah調試中的查詢報告不顯示正在保存的user_id。 – 2013-03-17 22:00:06

+0

但它作爲Profile => user_id存在於請求數據中?奇怪的。 – thaJeztah 2013-03-17 22:04:56

回答

5

這個語法是錯誤的:

echo $this->Form->input('User'); 

有一個字段名爲 「用戶」在您的Profile模型。這應該是:

echo $this->Form->input('user_id');