0

OK, I have a view template where I want to display an associated value from my 'item' entity:

<?= h($item->itemgroup->groupname)?>

If the $item->itemgroup_id is NULL, I get the error:

Trying to get property of non-object

It also errors without the h() function. However, if I change the view code to:

<?= h($item['itemgroup']['groupname']) ?>

It does not error, and displays a blank as expected.

Is it necessary to update all the baked view template code where a value is potentially NULL? Or is it a matter of database setup (i.e., not using NULL for a field that can be potentially blank)?

Thanks in advance for any insight or advice?

Cheers, D.

1 Answer 1

1

Is it necessary to update all the baked view template code where a value is potentially NULL? Or is it a matter of database setup (i.e., not using NULL for a field that can be potentially blank)?

Yes, it is.

To avoid error, just use :

<?=$item->itemgroup!==null ? h($item->itemgroup->groupname) : ''?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Yosi. Still I'm not sure why <?= h($item['itemgroup']['groupname']) ?> works, while the other way h($item->itemgroup->groupname) fails. I suppose the way you propose is safer.

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.