1

私はここで私の頭を壊しています。エラーの内容を確認していただければ幸いです。PHPActiveRecord をスパークを通じて CodeIgniter にインストールしましたが、1 つのことを除いてすべてがうまく機能します。コードをいくつかお見せしましょう。

これは私の問題のあるコントローラーです。

モデル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;
    }
}

そして、これはエラーが表示される部分です。コントローラの article.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');

Article::updater($id, $new_info) を呼び出すと、大きな厄介なエラーが表示されます。

致命的なエラー:非オブジェクトでのメンバー関数 update_attributes() の呼び出し

最も奇妙なことは、同じ機能を持つcategoy.phpというモデルとcategoy.phpという名前のコントローラーがあり(記事のカテゴリ機能をコピーして貼り付けた)、今回は機能しないことです。

モデル Article.php 内にさまざまな機能があり、それらはすべて正常に動作します。その Article::updater 部分に苦労しています。

行を適切に更新する方法を知っている人はいますか? PHP AR サイトのドキュメントに記載されているとおりに使用していますが、そのエラーが発生しています。それがオブジェクトではないと言うのはなぜですか?$article = Article::find($id) を実行するとオブジェクトになるはずです。

たぶん、本当に簡単なことを見ていないのでしょう。コンピューターの前にいる時間が長すぎます。

ありがとうアミーゴ。

4

2 に答える 2

3

変更する必要があります:

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);
于 2012-07-25T04:25:50.437 に答える
2

関数アップデータは static とマークする必要があり、$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-25T04:24:19.577 に答える