This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Colbydude
Colbydude

Hey all,

I'm having an issue after updating October to some more recent builds. I get this error on two separate installations across two different plugins. The installations are at build 306 and build 309. The issue is that when trying to reference a component's relationship with an object (hasOne/hasMany/belongsTo etc.), it is being returned null. This used to work on earlier builds, I'm not sure which build broke it.

The following is an example of the code:

function onEnd()
{    
    // Use product information for SEO.
    $this->page->title = $this->page->components['shopProduct']->product->title . ' | My Website';
    $this->page->canonical_url = 'https://mywebsite.com/merch/product/' . $this->page->components['shopProduct']->product->slug; 
}

The goal here is to inject the name of a product into the page's meta title and so on. However, $this->page->components['shopProduct']->product now returns null, so the above code throws a "Trying to get property of non-object" error.

Any information or possible workarounds on this would be greatly appreciated. I can provide any additional information if needed as well. Thanks!

Last updated

alxy
alxy

Try the following:

$this->shopProduct->product->title

where shopProduct is the components alias.

Colbydude
Colbydude

Sorry for the delayed response. Unfortunately doing that yielded the same result. =\

alxy
alxy

For the RainLab.Blog plugin this approach works...

Is the component propert (product) defined as public $product in your component class?

Colbydude
Colbydude

Ah, it was not! Working now. :)

Is that methodology something recently added in later builds?

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.