2012-05-19 57 views
5

我以yii開頭,嘗試使用條件過濾CActiveDataProvider時遇到了一些麻煩。用yii中的條件檢索CActiveDataProvider

這是我的數據庫模型:

Table   Columns 
Project   [id, name, status] 
userToProject [user_id,project_id,role] 
User    [id , ....] 

而且我喜歡指數行動來顯示所有與狀態=完成或者用戶當前分配給該項目的項目。

因此爲測試目的硬編碼用戶標識。這是我的代碼:

$criteria=array(
      'order'=>'status desc', 
      'with'=>array(
       'userToProject','userToProject.user'=>array('alias'=>'user')), 
    ); 
$criteria['condition']='status=='.Project::STATUS_FINISHED; 
$criteria['condition'].=' OR user.id = 6'; 
$dataProvider=new CActiveDataProvider('Project', array(
      'criteria'=>$criteria, 
    )); 
$this->render('index',array(
      'dataProvider'=>$dataProvider, 
    )); 

但它拋出一個異常說未知列「USER.ID。我錯過了什麼?由於

編輯:錯誤代碼是:

Column not found: 1054 
Unknown column 'users.user_id' in 'where clause'. The SQL statement executed was: 
SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `t`.`description` AS `t0_c2`, 
`t`.`status` AS `t0_c3`, `t`.`creation_date` AS `t0_c4` FROM `pgp_project` `t` WHERE 
    (status=4 OR users.user_id=6) ORDER BY status desc LIMIT 10 
+1

提供完整的錯誤信息,包括觸發它的SQL查詢。 – DCoder

+0

是的確切的SQL和錯誤消息將需要 –

+0

我沒有觸發器在表中。我要再次檢查錯誤 – Addev

回答

14

試試這個

$criteria=new CDbCriteria(array(     
    'order'=>'status desc', 
    'with' => array('userToProject'=>array('alias'=>'user')), 
    'condition'=>'status='.Project::STATUS_FINISHED.' OR user.id = 6', 
)); 

$dataProvider=new CActiveDataProvider('Project', array(
    'criteria'=>$criteria, 
)); 

$this->render('index',array(
    'dataProvider'=>$dataProvider, 
)); 
相關問題