Problem with user object / user collection

I have a collection of users, consisting out of valid user objects.

$areaChiefs

When I loop through this collection

      foreach ($areaChiefs as $chief): ?>
        <li>
          <?= $chief->email() ?>
        </li>
      <?php endforeach; ?>

I can access the user fields, for example email()

When I try to access a single user from this collection

$chief = $areaChiefs->findBy('username', 'George')

I get an error, when trying to access the user fields:

$chief->email()
Call to a member function email() on null

When I var_dump($chief), I get a valid user object:

object(Kirby\Cms\User)#446 (7) { ["avatar"]=> object(Kirby\Cms\File)#175...

Any idea? I already searched through the forum and the docs, but are totally stuck.

Many thanks in advance!

If the user object exists, the call the email field should work. However, it is good practice to always, always, always make sure you have an object of a class before you call a member method, by wrapping your code within an if statement. Then you can be sure that you don’t get an error in case the object/user does not exist:

<?php if ($chief = $areaChiefs->findBy('username', 'George')) {
  echo $chief->email();
}
?>
1 Like

Dear Sonja,

thank you again. It works, if I prepend the if statement. But I really don´t understand, what´s the difference between

<?php if ($chief) {
  echo $chief->email();
}
?>

and

<?= $chief->email() ?>

here. The 2nd variant throws the "Call to a member function email() on null" error on the same user object $chief.

Hm, could you provide the complete context of your code?

Will try to do this tomorrow. It is a client project, therefore I will have to do some “anonymizing” before.

You can always send as private message.

Hi Sonja,

thank you for your offer to take a closer look at the problem I described:

The problem occurs in Kirby versions 3.6 and 3.6.1.1. The affected site is running locally in my development environment under Laravel Valet.

The site is developed for a non-profit organization working in several European countries. Each country is divided into several regional areas. Each member of the organization/user of the site is assigned to a country and a region. This is done with corresponding fields for the users.

----

memberCountryGroupCode: DE

----

memberAreaCode: WST

Each member can perform one ore more functions in the country. These functions are assigned to the members/users via a tag field

----

memberCountryFunction: chief

There is one page per country, and one page per regional area. The pages for the regional areas are children of the pages for the countries.

The problem I described occurs in the template for the country pages. They should contain a table of the children regional areas which also contains the users with function in the areas.

In the page controller for the country page I create a collection of all users with function in the country:

  $allChiefs = $kirby
    ->users()
    ->filterBy('memberStatus', '==', 'aktiv')
    ->filterBy('memberCountryGroupCode', '==', $page->countryCode())
    ->filterBy('memberCountryFunction', '==', 'chief');

Furthermore a collection of the children regional areas

		$countryAreas = $page
			->children()->listed()->sortBy('areaCode', 'asc');

In the template, I loop through the regional areas and display the users with function in the respective areas (for the example here only one function per area)

       <?php foreach ($countryAreas as $area): ?>
        <li>
          <h2><?= $area->areaCode() ?></h2>
          <?php $chief = allChiefs
          	->findBy('memberAreaCode', $area->areaCode()->value()); ?>
          <?= $chief->memberLastName() ?>
        </li>
      <?php endforeach; ?>

Here, the error appears.

When I use

      <?php if ($chief) {
        echo $chief->memberLastName();
        } ?>

the error disappears.

The problem is that you are doing this inside a loop and if only one of the chiefs fail, you get the error. That’s why I wrote above that such things should always be wrapped in an if statement. You can never be sure that an object exists when the code runs (users, pages etc. might be deleted, renamed…).

That´s it, thanks a lot!

I can reproduce the error now. The underlying problem was in the controller, that only gave me 5 chiefs where I needed 6 chiefs to populate all areas