我無法確定是否有更好的方法來過濾數組收集 。 這是我的代碼:使用預先計算的原則過濾
$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;
}
);
這將返回從$所有用戶獲取已收集過濾SearchResult所。
我需要獲取所有用戶表單數據庫,其位置不超出用戶輸入他的個人資料和輸入搜索表單中的城市的距離。爲了確定是否超出距離,我必須計算搜索表單中輸入的位置與用戶位置之間的距離,並將該距離與用戶配置文件距離進行比較。
是否可以在數據庫級別進行這種過濾?我查看了Criteria API,但無法弄清楚如何去做。
感謝您的幫助。我編輯了這個問題以獲得更好的解釋請確認您的建議是否仍然適用。 – 3ND
是的建議仍然適用,我稍微更新了我的問題。將「經度」和「緯度」列添加到您的表中將允許您使用距離進行搜索。請參閱我的答案中的鏈接,瞭解如何做到這一點的示例。 – Wilt
會做。非常感謝。 – 3ND