1

I have a loaded Doctrine entity in my PHP application.

How do I check if Doctrine's array collection is already populated with data without triggering the database request?

I need this in my entity serialization logic.


I'm not sure that proposed solution of using isEmpty() is a good one, because I don't know whether it triggers database call or not. Does it?

Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202
  • Possible duplicate of [Check if ArrayCollection is empty](http://stackoverflow.com/questions/17643047/check-if-arraycollection-is-empty) – Jake N Nov 19 '16 at 18:56
  • The collection could be empty in two cases, when there are no related entities in the database and when the related entities are not yet loaded. I'm interested in the second case. And I'm not sure that provided answer, i.e. `isEmpty()` actually triggers the database call or not. – Slava Fomin II Nov 19 '16 at 19:06

1 Answers1

0
public function isCollectionPopulated()
{
    if ($this->collection instanceof PersistentCollection) {
        return $this->collection->isInitialized();
    }

    // else collection instance of ArrayCollection and already populated.
    return true;
}
d.garanzha
  • 813
  • 1
  • 9
  • 15