2

Having the following Models:

news.php

class News extends Aware {

    public static $table = 'noticia';
    public static $key = 'idnoticia';
    public static $timestamps = false;

    public static $rules = array(
        'titulo' => 'required',
        'subtitulo' => 'required',
    );

    public function images()
    {
        return $this->has_many('Image');
    }
}

image.php

class Image extends Aware {

    public static $timestamps = true;

    public static $rules = array(
        'unique_name' => 'required',
        'original_name' => 'required',
        'location' => 'required',
        'news_id' => 'required',
    );

    public function news()
    {
        return $this->belongs_to('News');
    }

}

Then in a controller I do the following:

$image = new Image(array(
    'unique_name' => $fileName,
    'original_name' => $file['file']['name'],
    'location' => $directory.$fileName,
    'news_id' => $news_id,
));
News::images()->insert($image);

I keep getting the following error message:

Non-static method News::images() should not be called statically, assuming $this from incompatible context

Any ideas what am I doing wrong?

Setting public static function images() doesn't seem to be wanted, as after a refresh I get an error saying

$this when not in object context

Gordon said that by doing News::images()->insert($image); I'm doing a static call, but that's how saw to do it

Alex
  • 7,538
  • 23
  • 84
  • 152

3 Answers3

3

You're using $this in a function that is called statically. That's not possible.

$this becomes available only after you create an instance with new.

If you turn on strict mode you will get another error, namely that images is not a static function and thus shouldn't be called statically.

The problem is in News::images(), not in images()->insert($image);

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • same as I told Martin, I know that... I tried that as I was told to do so, even though knowing that would have the mentioned issue. – Alex Jan 03 '13 at 09:39
3

You are missing some steps.

The Image belongs to News, but you're not referencing the News post you want to update.
You probably want to do:

$image = new Image(array(...));
$news = News::find($news_id);
$news->images()->insert($image);

More in the docs.

Nicholas Ruunu
  • 603
  • 5
  • 12
1

$this can only be used within an object instance. Class::method() calls a static method of the specified class.

In your case, you mixed both.

Your function definition for images is for an object instance:

public function images()
{
    return $this->has_many('Image');
}

You are calling it as a static method:

News::images()->insert($image);

The News class would need to be instantiated or the images method be modified to support static calls.

Martin Samson
  • 3,970
  • 21
  • 25
  • if you didn't notice... I said that I had tried using `public static...` as I was recommended to do so, but obviously would have the mentioned issue – Alex Jan 03 '13 at 09:38
  • Yeah, hence why I mentioned you can only use `$this` within an object instance. – Martin Samson Jan 03 '13 at 16:58