Skip to content
Advertisement

Doctrine 2: Load entity and childs count

I have these two entities:

Message entity

JavaScript

User entity

JavaScript

I need to get the total of messages of user and its data in order to echo something like this:

JavaScript

I now that I can make a relation in User entity to get a message collection. But I don’t know if it necessary only to get the size.

Advertisement

Answer

Well, I found the answer. At least one good option.

As of Doctrine 2.1 you can mark associations as extra lazy. This means that calling $user->getMessages()->count() won’t load the messages, it will just issue a COUNT query to the database.

You can read about extra lazy collections here: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/tutorials/extra-lazy-associations.html

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement