<?php
class ProfileRepository extends EntityRepository{
public function getProfilesExperimental($searchString, $isMapSearch = false){
$searchString = strtolower($searchString);
$em = $this->getEntityManager();
$cit = $em->getRepository('LunnarBundle:City')->findAll();
$cat = $em->getRepository('LunnarBundle:Profilecategory')->findAll();
$bra = $em->getRepository('LunnarBundle:Brand')->findAll();
$cities = array();
$profilecategories = array();
$brands = array();
foreach($cit as $city){
array_push($cities, strtolower($city->getName()));
}
foreach($cat as $category){
array_push($profilecategories, strtolower($category->getName()));
}
foreach($bra as $brand){
array_push($brands, strtolower($brand->getName()));
}
$parameters = array();
//'var' => '%'.$searchString.'%',
$searchQuerySelectJoin = "SELECT p FROM LunnarBundle:Profile p LEFT JOIN p.divisions d ";
$searchQueryWhere = "";
//////////////////////////
$searchStringArray = explode(" ", $searchString);
// Remove í, á and við
$removeWords = array('í','á','við');
//Find city, category, brand and save for later, and remove them from $searchStringArray
$searchStringArray = array_diff($searchStringArray, $removeWords);
$city = array_intersect($searchStringArray, $cities);
$searchStringArray = array_diff($searchStringArray, $city);
$category = array_intersect($searchStringArray, $profilecategories);
$searchStringArray = array_diff($searchStringArray, $category);
$brand = array_intersect($searchStringArray, $brands);
$searchStringArray = array_diff($searchStringArray, $brand);
//// PRINTS
print_r($searchStringArray);
echo '<br/>';
//print_r($city);
//print_r($profilecategory);
//print_r($brand);
$first = true;
$searchTerms = " ";
if (sizeof($searchStringArray)>0){
$searchTerms = current($searchStringArray);
$parameters['var'] = "%".$searchTerms."%";
$searchQueryWhere = "WHERE ( p.name LIKE :var ".$searchQueryWhere;
$first = false;
} elseif (sizeof($searchStringArray)==0){
$searchQueryWhere = " WHERE ( ".$searchQueryWhere;
}
if ($category){
$parameters['category'] = $category;
$searchQuerySelectJoin .= " LEFT JOIN p.profilecategory pc ";
if (!$first){
$searchQueryWhere .= " AND pc.name = :category ";
}else {
$first = false;
$searchQueryWhere .= " pc.name = :category ";
}
}
if ($brand){
$parameters['brand'] = $brand;
$searchQuerySelectJoin .= " LEFT JOIN p.brand b ";
if (!$first){
$searchQueryWhere .= " AND b.name = :brand ";
}else {
$first = false;
$searchQueryWhere .= " b.name = :brand ";
}
}
if ($city){
$parameters['city'] = $city;
$searchQuerySelectJoin .= " LEFT JOIN d.city c ";
if (!$first){
$searchQueryWhere .= " AND c.name = :city ";
}else {
$first = false;
$searchQueryWhere .= " c.name = :city ";
}
}
//////////////////////////
if($isMapSearch == true){
$searchQuery .= "AND d.latitude IS NOT NULL ";
}
$searchQuery = $searchQuerySelectJoin.$searchQueryWhere." ) ORDER BY p.name ASC";
echo $searchQuery;
$query = $this->getEntityManager()
->createQuery(
$searchQuery
)->setParameters($parameters);
return $query->getResult();
}
}
preferences:
44.19 ms | 402 KiB | 5 Q