我在这里摔碎了头。希望你能看到什么是错误。我通过星星之火将PHPActiveRecord安装到CodeIgniter上,除了一件事外,一切都很好。让我给你看看密码。
这是我的问题控制器。
Article.php模型
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Article extends ActiveRecord\Model
{
static $belongs_to = array(
array('category'),
array('user')
);
public function updater($id, $new_info)
{
// look for the article
$article = Article::find($id);
// update modified fields
$article->update_attributes($new_info);
return true;
}
}这部分显示了我的错误。Controller articles.php中的相关代码
// if validation went ok, we capture the form data.
$new_info = array(
'title' => $this->input->post('title'),
'text' => $this->input->post('text'),
'category_id' => $this->input->post('category_id'),
);
// send the $data to the model
if(Article::updater($id, $new_info) == TRUE) {
$this->toolbox->flasher(array('code' => '1', 'txt' => "Article was updated successfully."));
} else {
$this->toolbox->flasher(array('code' => '0', 'txt' => "Error. Article has not been updated."));
}
// send back to articles dashboard and flash proper message
redirect('articles');当我调用文章::updater($id,$new_info)时,它会显示一个大的恼人错误:
致命错误:对非对象上的成员函数update_attributes()的调用
最奇怪的一点是,我有一个名为categories.php和model Categoy.php的控制器,它具有相同的功能(我复制粘贴了文章的类别功能),但这一次不能工作。
在模型Article.php中,我有不同的功能,它们都很好地工作,我一直在挣扎于这篇文章::updater部分。
有人知道如何正确更新一行吗?我正在使用PHP AR站点中的docs,这给了我这个错误。为什么它说那不是一个物体?当我执行$article =子句::find($id)时,它应该是一个对象。
也许我看不出什么很容易的东西。在电脑前的时间太多了。
谢谢朋友们。
发布于 2012-07-25 04:24:19
函数更新程序需要标记为静态,当$id不好时,它应该处理错误条件。
public static function updater($id, $new_info)
{
// look for the article
$article = Article::find($id);
if ($article === null)
return false;
// update modified fields
$article->update_attributes($new_info);
return true;
}发布于 2012-07-25 04:25:50
你需要改变:
public function updater($id, $new_info)
{
// look for the article
$article = Article::find($id);至:
public static function updater($id, $new_info)
{
// look for the article
$article = Article::find($id);https://stackoverflow.com/questions/11642664
复制相似问题