2014-09-26 40 views
1

我有兩個表店(ID,姓名,日期)store_services(ID,STORE_ID,名稱,價格,日期) 我已經實現搜索過濾價格,我想在我的網頁上顯示獨特的商店記錄。 但我仍然從商店表中獲取重複記錄。如何添加不同的外鍵和主鍵Yii中CdBCriteria

它的工作在MySQL查詢精由不同的(store_services.store_id) ,但它不是在的Yii CDbCriteria工作。 我的MySQL查詢:

SELECT DISTINCT(store_services.store_id),store.id,store.name,store.date FROM store INNER JOIN store_services ON store.id = store_services.store_id WHERE store_service.price BETWEEN 1,1000 

請給我Yii的不同記錄的代碼 注:我使用Yii中$基準 - >獨特= TRUE;

,但它也越來越重複記錄

+1

刪除鮮明並嘗試使用GROUP BY像'$基準 - >組=「STORE_ID」同列;' – 2014-09-26 07:06:05

+0

由於它的工作 – aman 2014-09-26 07:15:38

+0

我建議在這裏粘貼代碼,請確保您使用的findAll() ; – Rorschach 2014-09-26 07:15:54

回答

1

它的工作更換$基準 - >獨特= TRUE;$ criteria-> group ='store_id';

這是我的所有代碼希望有人從這裏找到幫助。

 $criteria=new CDbCriteria; 
    //$criteria->distinct=true; 
    $criteria->group = 'store_id'; 
    $criteria->select = 't.id,t.name,t.state,t.city,t.location,t.address,t.contact_no,t.email,t.facilities,t.profile_photo,t.description, t.merchant_id, t.approve, t.added_date'; 
    $flag = false; 

    if(isset($_GET['Store']['category']) && !empty($_GET['Store']['category'])){ 
     $criteria->compare('mmmStoreServices.category_id', $_GET['Store']['category']); 
     $flag = true; 
    } 

    if(isset($_GET['Store']['sub_category']) && !empty($_GET['Store']['sub_category'])){ 
     $criteria->compare('mmmStoreServices.service_id', $_GET['Store']['sub_category']); 
     $flag = true; 
    } 



    if(isset($_GET['Store']['price']) && !empty($_GET['Store']['price'])){ 
     $price = explode('-',$_GET['Store']['price']); 
     $minPrice = trim($price[0]); 
     $maxPrice = trim($price[1]); 
     $criteria->addBetweenCondition('mmmStoreServices.price', $minPrice, $maxPrice); 
     $flag = true; 

    } 


    if($flag){ 
     $criteria->with = array('mmmStoreServices'); // Put `mmm_store_service` to relations of model 'Store' 
     $criteria->together = true; // Check if you really need this parameter! 
    } 

    if(isset($_GET['Store']['location']) && !empty($_GET['Store']['location'])){ 
     $criteria->compare('t.location', $_GET["Store"]["location"]); 
     //$flag = true; 

    } 


    $criteria->compare('t.approve', 'Y'); 

    $ajaxModel = new CActiveDataProvider('Store', array(
     'criteria' => $criteria, 
     'pagination' => array('pageSize'=>'2'), 
    ));