dtmm0148603 2014-09-24 22:36
浏览 37
已采纳

yii Cdbcriteria无法正常工作

I want to fetch data from two tables Store and mmm_store_services it's all conditions working fine except this

  1. if(isset($_POST['Store']['location']) && !empty($_POST['Store']['location'])){
  2. $conditions[] = ' AND t.location = '.$_POST["Store"]["location"];
  3. }

here is my all code

  1. $criteria=new CDbCriteria;
  2. $conditions = array();
  3. if(isset($_POST['Store']['category']) && !empty($_POST['Store']['category'])){
  4. $conditions[] = ' AND mmm_store_services.category_id ='.$_POST['Store']['category'];
  5. }
  6. if(isset($_POST['Store']['sub_category']) && !empty($_POST['Store']['sub_category'])){
  7. $conditions[] = ' AND mmm_store_services.service_id ='.$_POST['Store']['sub_category'];
  8. }
  9. if(isset($_POST['Store']['location']) && !empty($_POST['Store']['location'])){
  10. $conditions[] = ' AND t.location = '.$_POST["Store"]["location"];
  11. }
  12. if(isset($_POST['Store']['price']) && !empty($_POST['Store']['price'])){
  13. $price = explode('-',$_POST['Store']['price']);
  14. $minPrice = trim($price[0]);
  15. $maxPrice = trim($price[1]);
  16. $conditions[] = ' AND mmm_store_services.price between '.$minPrice.' AND '.$maxPrice;
  17. }
  18. if(count($conditions)>0){
  19. $condition = implode(' ',$conditions);
  20. $criteria->join.='INNER JOIN mmm_store_services ON mmm_store_services.store_id = t.id '.$condition;
  21. }
  22. $criteria->compare('t.approve','Y');
  23. $model = new CActiveDataProvider('Store', array(
  24. 'criteria'=>$criteria,
  25. 'pagination'=>array('pageSize'=>'2'),
  26. ));

please give me a solution. thanks.

展开全部

  • 写回答

1条回答 默认 最新

  • duan7772 2014-09-24 22:55
    关注

    If your $conditions must be in WHERE part and not in JOIN part, than you are missing WHERE keyword and using CDbCriteria wrong (basically not using it at all).

    $criteria = new CDbCriteria();
    $flag = false;
    
    if(isset($_POST['Store']['category']) && !empty($_POST['Store']['category'])){
        $criteria->addCondition(['mmm_store_services.category_id' => $_POST['Store']['category']]);
        $flag = true;
    }
    
    if(isset($_POST['Store']['sub_category']) && !empty($_POST['Store']['sub_category'])){
        $criteria->addCondition(['mmm_store_services.service_id' => $_POST['Store']['sub_category']]);
        $flag = true;
    }
    
    if(isset($_POST['Store']['location']) && !empty($_POST['Store']['location'])){ 
        $criteria->addCondition(['t.location' => $_POST["Store"]["location"]]);
        $flag = true;
    }
    
    if(isset($_POST['Store']['price']) && !empty($_POST['Store']['price'])){
        $price = explode('-',$_POST['Store']['price']);
        $minPrice = trim($price[0]);
        $maxPrice = trim($price[1]);
        $criteria->addBetweenCondition('mmm_store_services.price', $minPrice, $maxPrice);
        $flag = true;
    }
    
    
    if($flag){
        $criteria->with = ['mmm_store_service']; // Put `mmm_store_service` to relations of model 'Store'
        $criteria->together = true; // Check if you really need this parameter!
    }
    
    $criteria->compare('t.approve', 'Y');   
    
    $model = new CActiveDataProvider('Store', array(
        'criteria' => $criteria,
        'pagination' => array('pageSize'=>'2'),
    ));
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部