1
votes

I have two tables store(id,name,date) and store_services(id,store_id,name,price,date) I have implemented search filter by price, i wanted to show unique store records in my page. But still i'm getting duplicate records from store table.

It's Working fine in mysql query by distinct(store_services.store_id) but it's not working in Yii CDbCriteria. my Mysql query is :

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

Please give me a code of Yii distinct records NOTE: I'm using in Yii $criteria->distinct=true;

but it's also getting duplicate records

1
Remove Distinct and try using group by on same column like $criteria->group = 'store_id'; - Ratan K
I would suggest paste the code here, make sure you are using findAll(); - Wit Wikky
I make answer with my full code.you would like to see that. - aman

1 Answers

1
votes

It's working replacing $criteria->distinct=true; to $criteria->group = 'store_id';

Here is my All code hope someone finding help from this.

        $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'),
    ));