Check if ArrayCollection is empty

ChrisS picture ChrisS · Jul 14, 2013 · Viewed 33k times · Source

I have an Entity Order which hold Suppliers in an Arraycollection. In my controller i want to check if this arraycollection is empty:

$suppliers = $order->getSuppliers();

I tried:

if(!($suppliers)) {}
if(empty($suppliers)) {}

Any ideas?

Answer

Ken Hannel picture Ken Hannel · Jul 14, 2013

Doctrine ArrayCollection has a method isEmpty that will do what you are looking for.

if ($suppliers->isEmpty()) { }

Take a look at the documentation for it here