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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.
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.

2 Comments

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
Yeah, hence why I mentioned you can only use $this within an object instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.