gpt4 book ai didi

php - CakePHP 外键未保存在 belongsTo 关系中

转载 作者:行者123 更新时间:2023-12-02 05:11:01 26 4
gpt4 key购买 nike

我有一个表 profiles 和一个表 usersprofiles 有一个 belongsTo 关系>用户。在我的 profiles/edit View 中,我有一个现有 users 的下拉列表可供选择。

但是,users.id 并没有像我期望的那样保存在 profiles.user_id 中。

ProfilesController.php -> 编辑:

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

然后在 View -> 配置文件 -> edit.ctp

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

在 Controller 中运行 debug($this->request); 显示正确的值被发送回 Controller 。保存操作如下所示:

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',
...

最佳答案

语法错误:

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

没有在您的Profile 模型中有一个名为“User”的字段。这应该是:

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

关于php - CakePHP 外键未保存在 belongsTo 关系中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15466528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com