Good day,
I have a class Cars and class CarTypes in a Many-To-Many relation, Cars contain an ArrayCollection of CarTypes. I want to search for Cars satisfying any of multiple selected CarTypes (passed from form in array).
Till now I can only think of three nested loops, 1st over Cars, 2nd inner loop over ids array (from form) and 3rd inner loop on ArrayCollection of CarTypes in Cars to check if value from 2nd loop exists in 3rd.
I have already checked below links related to closure. But these or none of the links in docs provide sufficient details on how to match an array of selected ids against ids from ArrayCollection.
Doctrine 2 ArrayCollection filter method
Doctrine 2, query inside entities
I have Custom Repositories but feel it better to implement this in Cars class using exists of ArrayCollection, maybe something like following:
public function existsCarTypes($ctArray)
{
$CarTypes = $this->CarTypes;
return $CarTypes->exists(
function($CarTypes) use ($ctArray)
{
if (in_array($ctArray , $CarTypes->getId() ))
{
return true;
}
return false;
}
);
}
But this is not working, Is there a better way to do this, please provide any good documentation links.