Comparision of objects - Proxy Classes

Hi at all,

dont know, if I’m right here, but it want to give it a try.

I think, that my problem has to do with Lazy Loading, but I don’t know exactly.

I want to compare two Objects of the same type in one of my controllers (second line).

foreach ($stall->getApplications() as $application) {
    if ($this->companyRegistrationEvent === $application->getEvent()) {
        if ($application->hasFlag(Application::FLAG_ACTIVE)) {
            $applications->add($application);
        }
    }
}

The one object in the comparision is from an ArrayCollection ($stall->getApplications()), the other one is saved in the controller. In the code above, the comparision is never true, because $application->getEvent() is a Proxy-Class. But If I’m compare the persistence identifiers of the both objects, the result is true.

Now my question: why I can’t compare objects this way? Where is my mistake?

Thanks and kind regards
Eric

I’m not sure, but you’re comparing two objects when you really want to compare their types. Try if (get_class($this->companyRegistrationEvent) === get_class($application->getEvent()).

Sorry, don’t know, what you mean.

I don’t want to compare the classnames, because I know, of which class the two objects are. I wan’t to check, if the two objects are the same instance.

If I’m calling get_class on the two comparision-objects, the one get_class return “Eskit\CompanyRegistration\Domain\Model\Event” the other get_class return “Neos\Flow\Persistence\Doctrine\Proxies_CG_\Eskit\CompanyRegistration\Domain\Model\Event”. It seems the proxy class wasn’t resolved

Because the object is fetched as a proxy, and not initialized unless used. And comparison is not “using” an object. It will probably work, if you have called any method on the object before doing the comparison.

A “workaround” thus could be to add an equals() method to the class, and call that, as in $$this->companyRegistrationEvent.equals($application->getEvent())

2 Likes

Hey Karsten, thanks for the answer, especially the part, that a comparision isn’t “using” an object.

The thing with “call an method on the object” before comparision I tried before I wrote the ticket. But it doesn’t work. The object stays the proxy object.

Now I tried the equals-method, but it doesn’t work too. Would it be possible, that it is an annotation problem?

If your objects are entities, the identifier should be the source of truth to say that two object are equal. And certainly more performant then comparing the full object.

And as said by @kdambekalns I love to encapsulate the logic in the domain model, if this is parts of model.

Well, strict comparison ($a === $b) of two objects should be really cheap as well, since it checks for object identity and doesn’t recurse into the properties in any way.