ChrisS
ChrisS

Reputation: 734

Check if ArrayCollection is empty

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?

Upvotes: 32

Views: 43647

Answers (2)

Ken Hannel
Ken Hannel

Reputation: 2748

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

Upvotes: 96

A.L
A.L

Reputation: 10483

You can also use the count() PHP function:

if (count($suppliers) < 1) { }

Upvotes: 7

Related Questions