Getting nested Express Association Entry selections, not seeming to work "Call to a member function getWhatever() on array"

Okay so I have three Express Objects.

First one is Schedules
Second is Tournaments
Third is Events

A Tournament is associated with An Event. A Schedule is associated with A Tournament.

So I’m not sure why this fails…


Produces “Call to a member function getEvents() on array”

Tournaments to Events, Many to One (Many Tournaments to One Event)

Schedules to Tournaments, Many to One (Many Schedules to One Tournament)

I’m not sure I’m doing anything wrong here…

First of all, you should use singular form when naming Express objects.
I advise you to change it, since your code will be too confusing to read otherwise.
Depending of relationship, those magic methods will use singular or plural method names (they mimic natural language). If you don’t change them, you will probably have hard times understanding some code.

What is an $item?

I don’t remember right now, if this type of relationship:

$tournaments = $item->getEntry()->getEventTournaments();

returns directly an “object” or “array of objects” (with one element).
So you could probably continue like:

$id = $tournaments[0]->getEvents()->getID();

or loop through $tournaments (though it doesn’t make much sense if you always expect one element).

$item is expected to be defined from a foreach loop “foreach ($result->getItems() as $item)” and $result is Schedules (Entries of them).

I had done some cursory testing with singular/plural, and maybe I can test further, but that didn’t seem to change the result.

I’m going to try your proposed work-around, however this does seem like it should work, considering the expectation that only ONE “Tournament” or “Event” should ever be selected at any one time in the association for the relevant Entry/Entries.

@andrew do you know if this is “working as intended” or… maybe a bug?

LOL so trying to use that variable created as an array produces…

Cannot use object of type DoctrineProxies_CG_\Concrete\Core\Entity\Express\Entry as array

This is… confusing…

So below is what “worked” as in didn’t throw an error:

$testTourn = $item->getEntry()->getEventTournaments();
if ($pageEventID != $testTourn->getEvents()->getID()) {

I haven’t reconfigured the Express Objects in any way, and yeah this is confusing…

Okay so I just want to add to this topic that the issue actually was an association configuration error on my part, as @Parasek had touched upon.

I thought two of the associations were correctly configured of ManyToOne, but they were actually configured as OneToMany, hence resulting in an Array when it was not expected.

Once I had corrected this (by correctly creating two associations as ManyToOne, and deleting the incorrectly configured associations) I was then able to dig deep in one query of “$item->getEntry()->getEventTournaments()->getEvents()->getID()” and other similar multi-level depths.

As for @Parasek 's conern about Singular vs Plural nomenclature, I have not yet seen where there is value in bothering having singular and plural nomenclature available. Maybe it’s in the way I work with Express Objects and Entries, but in-practice I don’t yet see the value.

Anyways, just wanted to add this detail in the hopes maybe it can help someone, by learning from my mistakes. :slight_smile:

1 Like