【发布时间】:2014-05-02 12:08:01
【问题描述】:
在 LAMP 设置(Ubuntu 13.10、Apache 2、MySQL Server 5.5、PHP 5.5.3)上使用 CakePHP 2.4.9。
试图将模型(用户)打包到插件中,但我遇到了一个奇怪的问题:
在两个不同的操作中,我使用save() 创建或更新用户。这有效:
if ($this->request->data) {
if ($this->User->create() && $this->User->save($this->request->data)) {
$this->Session->setFlash(_('<strong>Congratulations!</strong> Successfully created a new user.'), 'default', array('class' => 'alert alert-success'));
$this->redirect(array('action' => 'register'));
} else {
$this->Session->setFlash(_('<strong>Oops!</strong> Could not create a new user.'), 'default', array('class' => 'alert alert-danger'));
}
}
但这不起作用:
if ($this->request->data) {
$this->User->id = $id;
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(_('<strong>Congratulations!</strong> Successfully updated your user account.'), 'default', array('class' => 'alert alert-success'));
$this->redirect(array('action' => 'settings'));
} else {
$this->Session->setFlash(_('<strong>Oops!</strong> Could not update user account.'), 'default', array('class' => 'alert alert-danger'));
}
}
当我尝试保存后一个示例(“更新操作”)时,出现以下错误:
致命错误
错误:在非对象上调用成员函数 getColumnType() 文件:/home/johan/sites/userplugin/lib/Cake/Model/Model.php 线路:1412
注意:如果要自定义此错误信息,请创建 app/View/Errors/fatal_error.ctp
表单非常标准,我使用FormHelper 创建表单和字段。但在更新表单中,我有一个带有 ID 的隐藏字段:
<?php echo $this->Form->hidden('id'); ?>
如果我删除它,更新表单会通过但会创建一个新对象!所以就像$this->User->id = $id 什么都不做。我目前设置$id“手动”,所以$id = 1...
我尝试搜索类似的问题,但没有找到任何东西。一个讨论提到了 ID 字段,也许它没有正确设置?但我找不到任何解决方案。
【问题讨论】:
-
Cake 不应该使用
ID而不是id(区分大小写)吗? -
不这么认为。手册说://更新:id设置为数值$this->Recipe->id = 2; $this->recipe->save($this->request->data); Link to manual
-
我的错误,ID 在其他框架中。对不起:)
-
错误消息表明您的模型关联可能有问题。另请注意,如果 ID 已经通过数据传递,则无需设置 ID,理想情况下主键列应自动递增。
-
我没有任何关联的模型。这可能与模型是插件的一部分有关吗?认为我可能在某处引用错误,这使得 CakePHP 假设
User是“应用程序空间”的一部分? id字段是AI,不管我是通过表单数据传递id还是用$this->User->id设置...
标签: php mysql cakephp cakephp-2.4