首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHPActiveRecord和CodeIgniter更新记录?

如何使用PHPActiveRecord和CodeIgniter更新记录?
EN

Stack Overflow用户
提问于 2012-07-25 04:19:46
回答 2查看 2K关注 0票数 1

我在这里摔碎了头。希望你能看到什么是错误。我通过星星之火将PHPActiveRecord安装到CodeIgniter上,除了一件事外,一切都很好。让我给你看看密码。

这是我的问题控制器。

Article.php模型

代码语言:javascript
复制
<?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中的相关代码

代码语言:javascript
复制
        // 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)时,它应该是一个对象。

也许我看不出什么很容易的东西。在电脑前的时间太多了。

谢谢朋友们。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-25 04:24:19

函数更新程序需要标记为静态,当$id不好时,它应该处理错误条件。

代码语言:javascript
复制
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;
}
票数 2
EN

Stack Overflow用户

发布于 2012-07-25 04:25:50

你需要改变:

代码语言:javascript
复制
public function updater($id, $new_info)
    {
            // look for the article
        $article = Article::find($id);

至:

代码语言:javascript
复制
public static function updater($id, $new_info)
    {
            // look for the article
        $article = Article::find($id);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11642664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档