craftql icon indicating copy to clipboard operation
craftql copied to clipboard

Check if getSetById exists when querying globals

Open jermashley opened this issue 6 years ago • 4 comments

Wrapping the $set in an if check fixed an issue querying globals that returned an error Trying to get property of non-object.

Issue #223

jermashley avatar Feb 11 '19 20:02 jermashley

This seems like a good fix. Although, I'm not totally clear on what issue it solves?

First we get all the set ids and loop over them,

$setIds = \Craft::$app->globals->getAllSetIds();
foreach ($setIds as $id) {

Then on each set we get the details of the set,

$set = \Craft::$app->globals->getSetById($id, $siteId);
$sets[$set->handle] = $set;

Since the code from the first blog grabs all the set ids why would the code from the second block ever not return a set?

I assume I'm missing something obvious here. Sorry for being dense.

markhuot avatar Feb 11 '19 21:02 markhuot

No worries. I'm not sure how to explain it as far as the code goes. I tested querying globals on a Craft install that only has one site. Things work as expected.

However, I get the error of Trying to get property of non-object when querying globals in a multi-site environment. Maybe something with the $siteId?

jermashley avatar Feb 11 '19 21:02 jermashley

@markhuot this seems to fix the same issue for me. $set is null/undefined at some point. Probably its something with the $siteId which is 'default' or 1 in my case. Unfortunately I have no time to setup a dev environment for craft and track it down myself.

Right now I am using "craftcms/cms": "3.1.14" with "markhuot/craftql": "1.3.1".

If you want to have a closer look I could provide you with my docker image + project.yaml - just let me know.

OFranke avatar Mar 04 '19 11:03 OFranke

👋🏼 @markhuot I apologize for getting all of the wires crossed with this PR. Is this the correct place you were referencing here?

jermashley avatar Mar 18 '19 14:03 jermashley