I'm having trouble figuring if there is a better way to filter array collection . This is my code:
$searchResults = $this->get('app.user.repository')->getUsersBySearchData($searchDetails);
$searchResults->filter(
function($user) use ($searchDetails){
$distance = $this->get('app.distance.calculator')->calculateDistance($user->getCity(), $searchDetails->getCity());
$distanceToTravel = $user->getDistanceToTravel();
if($distance < $distanceToTravel)
return true;
return false;
}
);
This returns all users from $searchResults filtered from already fetched collection.
I need to fetch all users form database whose location is not beyond distance which user enters in his profile and city entered in search form. To determine if that distance is not exceeded i have to calculate distance between location entered in search form to location of user and compare that distance to user profile distance.
Is it possible to do this kind of filtering on database level? I looked into Criteria API but couuldn't figure out how to do it.