问题
I got a big query that execute 11 query, I didnt know where the problem is, but found the problem was in a select a did for geo loc, anyone has an idea how to correct that?
It happens only when i use this query SfDoctrinePaginate
Investigating further $q->select("a.longitude") create as much queries..
The problem:
$q->select("a.longitude, a.latitude, (3959 * acos(cos(radians('".$lat."')) * cos(radians(latitude)) * cos(radians(longitude) - radians('".$long."')) + sin(radians('".$lat."')) * sin(radians(latitude)))) AS distance");
the complete model:
public function getListItems($orderby, $budget, $motscles, $userid, $catID, $useDistance = false)
{
$useDistance = true;
// CHANGE ORDER
if(!$orderby){
$orderby = "a.created_at DESC";
}else if($orderby == "price"){
$orderby = "a.price ASC";
}else if($orderby == "date") {
$orderby = "a.created_at DESC";
}else{
$orderby = "a.created_at DESC";
}
// Search Keywords in table
if($motscles){
$searchItem = Doctrine_Core::getTable('csw_Article');
$results = $searchItem->search($motscles);
$ids = array();
foreach ($results as $result) {
$ids[] = $result['id'];
}
if(sizeof($ids) == 0){
$ids[] = 0;
}
}
$q = Doctrine_Core::getTable('csw_Article')
->createQuery("a")
->leftJoin('a.csw_CategorieArticle ca');
$sfContext = sfContext::getInstance()->getUser();
if($useDistance){
$lat = (string)($sfContext->getAttribute('userLat')) ? $sfContext->getAttribute('userLat') : sfConfig::get("app_user_lat");
$long = (string)($sfContext->getAttribute('userLong')) ? $sfContext->getAttribute('userLong') : sfConfig::get("app_user_long");
$radius = 18;
$q->select("a.longitude, a.latitude, (3959 * acos(cos(radians('".$lat."')) * cos(radians(latitude)) * cos(radians(longitude) - radians('".$long."')) + sin(radians('".$lat."')) * sin(radians(latitude)))) AS distance");
$q->having("distance < ?", $radius);
}
if($orderby == "distance") {
$q->orderBy("distance desc");
}
$q->addOrderBy($orderby);
if($catID){
$q->where('ca.categorie_id = ?', $catID);
}
if($budget != 0){
$budget_min = $budget - ($budget * 0.20);
$budget_max = $budget + ($budget * 0.20);
$q->addwhere('a.price > ?',$budget_min)
->addwhere('a.price < ?',$budget_max);
}
if($userid){
$q->WhereIn('a.userid = ?', $userid);
}
if($motscles){
$q->whereIn('a.id', $ids);
}
$q->execute();
return $q;
}
回答1:
I would change all where by whereIn like:
if($userid){
$q->andWhereIn('a.userid', $userid);
}
if($catID){
$q->andWhereIn('ca.categorie_id', $catID);
}
I think this happens because when you're using the results in the view the paginator cant fetch all records in a row, so for each item has to do the query to get all fields.
回答2:
I am really not sure why but here what was the problem:
I used select('a.latitude')
when I should have used...
select('a.*')
来源:https://stackoverflow.com/questions/5073571/got-a-select-that-does-10-query-in-doctrine-symfony