Question

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.

Était-ce utile?

La solution

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

Autres conseils

You need to change:

public function updater($id, $new_info)
    {
            // look for the article
        $article = Article::find($id);

to:

public static function updater($id, $new_info)
    {
            // look for the article
        $article = Article::find($id);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top