2013-08-01 69 views
1

我想寫在警予下面的查詢:如何在yii中使用連接編寫查詢?

SELECT * FROM tbl_leaves as tl,tbl_users_master as um 
WHERE tl.user_id = um.user_id AND YEAR(tl.leave_from_date) = YEAR(NOW()) 

,我試圖寫警予如低於上述查詢:

$dataProvider=new CActiveDataProvider('Leaves', array(
     'criteria'=>array(
      'with'=>'user', 
      'together'=>true, 
      'condition'=>'user.user_id=:user_id AND YEAR(leaves.leave_from_date)= YEAR(NOW())', 
      'params'=>array(':user_id'=>$this->loadModel(Yii::app()->user->getId())->user_id),    
     ), 
    )); 
    $this->render('admin',array('model'=>$this->loadModel(Yii::app()->user->getId()), 
    'dataProvider'=>$dataProvider, 
    )); 

但它拋出我下面的錯誤:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: 
Column not found: 1054 Unknown column 'leaves.leave_from_date' in 'where clause'. 

執行的SQL語句是:

SELECT COUNT(DISTINCT `t`.`leave_id`) FROM `tbl_leaves` `t` 
LEFT OUTER JOIN `tbl_users_master` `user` ON (`t`.`user_id`=`user`.`user_id`) 
WHERE (user.user_id=:user_id AND YEAR(leaves.leave_from_date) = YEAR(NOW())) 

我是yii的新手,請幫助我如何在yii中編寫上述查詢。

回答

2

從錯誤中所有你需要做的就是調整你作爲一個別名錶

'condition'=>'user.user_id=:user_id AND YEAR(leaves.leave_from_date)= YEAR(NOW())', 
               ^--------- change to t 
名稱的外觀
相關問題