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

时间:2014-09-26 06:56:03

标签: php mysql yii

我有两个表商店(ID,名称,日期) store_services(id,store_id,名称,价格,日期) 我已经通过价格实施了搜索过滤器,我想在我的页面中显示唯一的商店记录。 但我仍然从商店表中获取重复记录。

它通过distinct(store_services.store_id)在mysql查询中正常工作 但它不适用于 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中使用 $ criteria-> distinct = true;

但它也会获得重复记录

1 个答案:

答案 0 :(得分:1)

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