2014-09-25 72 views
1

我想從兩個表商店mmm_store_services獲取數據是,除了這個警予Cdbcriteria不能正常工作

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

這裏做工精細的所有條件,就是我全部的代碼

 $criteria=new CDbCriteria; 

    $conditions = array(); 
    if(isset($_POST['Store']['category']) && !empty($_POST['Store']['category'])){ 
     $conditions[] = ' AND mmm_store_services.category_id ='.$_POST['Store']['category']; 
    } 

    if(isset($_POST['Store']['sub_category']) && !empty($_POST['Store']['sub_category'])){ 
     $conditions[] = ' AND mmm_store_services.service_id ='.$_POST['Store']['sub_category']; 
    } 

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

    if(isset($_POST['Store']['price']) && !empty($_POST['Store']['price'])){ 
     $price = explode('-',$_POST['Store']['price']); 
     $minPrice = trim($price[0]); 
     $maxPrice = trim($price[1]); 
     $conditions[] = ' AND mmm_store_services.price between '.$minPrice.' AND '.$maxPrice; 
    } 


    if(count($conditions)>0){ 
     $condition = implode(' ',$conditions); 
     $criteria->join.='INNER JOIN mmm_store_services ON mmm_store_services.store_id = t.id '.$condition; 
    } 



    $criteria->compare('t.approve','Y');  
    $model = new CActiveDataProvider('Store', array(
     'criteria'=>$criteria, 
     'pagination'=>array('pageSize'=>'2'), 
    )); 

請給我一個解決方案。 謝謝。

+0

'$ conditions'是否必須位於'WHERE'部分? – Justinas 2014-09-25 06:40:48

+0

那我該怎麼辦? – aman 2014-09-25 06:46:08

回答

0

如果您$conditions必須是WHERE一部分,而不是在JOIN部分,比你缺少WHERE關鍵字和使用CDbCriteria錯誤(基本上不使用它的話)。

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

我使用mmm_store_services作爲表格而不是關係。 – aman 2014-09-25 07:25:54

+0

@aman因此創建關係。 – Justinas 2014-09-25 07:27:51

+0

好的我正在處理你的代碼和關係,但是你的代碼中有一個syntex錯誤對不起找不到? – aman 2014-09-25 07:31:25