douji9518 2012-07-25 04:19
浏览 47
已采纳

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

Im breaking my head here. Hope you can see what's the error. I've installed PHPActiveRecord to CodeIgniter throught a spark and all works great except one thing. Let me show you some code.

This is my problematic controller.

Model 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;
    }
}

And this is the part where it shows me an error. Relevant code in 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');

When i call Article::updater($id, $new_info), it display a big annoying error:

Fatal error: Call to a member function update_attributes() on a non-object

The weirdest thing, is that i have a controller called categories.php and model Categoy.php that has the same functionality (i copy pasted the categories functionality for the articles) and this time, doesn't work.

I have different functions inside model Article.php and all of them work fine, im struggling with that Article::updater part.

Does somebody know for a way to proper update a row? Im using as stated at docs in PHP AR site and it's giving me that error. Why it does say that is not an object? It's supposed to be an object when i do $article = Article::find($id).

Maybe im not seeing something really easy. Too many hours in front of computer.

Thanks amigos.

  • 写回答

2条回答 默认 最新

  • dshgnt2008 2012-07-25 04:24
    关注

    Function updater needs to be marked static, and it should handle the error condition when $id is bad.

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算