如何在Yii CdBCriteria中添加外键和主键的区别


How to add distinct on foreign key and primary key in Yii CdBCriteria

我有两个表store(id,name,date)store_services(id,store_id,name,price,date)我已经通过price实现了搜索过滤器,我想在我的页面中显示唯一的商店记录。但我仍然从存储表中得到重复的记录。

通过distinct(store_services.store_id)在mysql查询中运行良好但它在YiiCDbWriter中不起作用。我的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中使用$criteria->distinct=true

但它也会得到重复的记录

它正在替换$criteria->distinct=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'),
    ));