Reputation: 373
I've array of objects
$states = $this->getDoctrine()->getRepository(LocationState::class)->findAll();
How can I check if $states
contains object with data?
LocationState {#102960 ▼
-id: 1
-ident: "02"
-name: "NAME"
-country: LocationCountry {#102992 ▶}
}
This is no ArrayCollection but Array of Objects.
Upvotes: 1
Views: 3802
Reputation: 4210
For array of objects:
$found = !empty(array_filter($objects, function ( $obj ) {
return $obj->name == 'NAME' && $obj->id == 1;
}));
For ArrayCollection:
$found = $objects->exists(function ( $obj ) {
return $obj->name == 'NAME' && $obj->id == 1;
});
Upvotes: 1
Reputation: 8162
If you want them to be retrieved by the query:
$this->getDoctrine()->getRepository(LocationState::class)
->findBy(['name' => 'NAME', 'ident' => '02']);
If you just want to know if a specified object is on your collection, you will have to use some code
$states = $this->getDoctrine()->getRepository(LocationState::class)->findAll();
$found = false;
foreach($state in $states) {
if($state->getName() == 'NAME' && $state->getIdent() == '02' ) {
$found = true;
}
}
Doctrine 2 ArrayCollection filter method
Upvotes: 0