2013-03-17 54 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');