2012-05-23 101 views
5

搜索我在我的數據庫3個表,Yii的CGridView排序,並與關係表

圖片低於,我按照http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

拿到最後一格與搜索和排序的目的。所以我必須連接2個表中search()功能像

public function search() 
    { 
     // Warning: Please modify the following code to remove attributes that 
     // should not be searched. 

     $criteria=new CDbCriteria; 

       $criteria->with = array('bposite'); 
       $criteria->together = false; 
       $criteria->with = array('client'); 
     $criteria->compare('id',$this->id); 
//  $criteria->compare('client_id',$this->client_id); 
//  $criteria->compare('bposite_id',$this->bposite_id); 
     $criteria->compare('userid',$this->userid,true); 
     $criteria->compare('password',$this->password,true); 
       $criteria->compare('bposite.name', $this->bposite_search, true);     
       $criteria->compare('client.name', $this->client_search, true); 


     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
          'sort'=>array(
           'attributes'=>array(
            'client_search'=>array(
             'asc'=>'client.name', 
             'desc'=>'client.name DESC', 
            ), 
            'bposite_search'=>array(
             'asc'=>'bposite.name', 
             'desc'=>'bposite.name DESC', 
            ), 
            '*', 
           ), 
          ), 
    )); 
    } 
} 

但我得到的結果對於客戶,bposite SEACH和排序是顯示像

>  Error 500: <h1>CDbException</h1> 
>  <p>CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bposite.name' 
> in 'where clause'. The SQL statement executed was: SELECT 
> COUNT(DISTINCT `t`.`id`) FROM `clientbposites` `t` LEFT OUTER JOIN 
> `client` `client` ON (`t`.`client_id`=`client`.`id`) WHERE 
> (bposite.name LIKE :ycp0) 
> (D:\wamp\www\yi\framework\db\CDbCommand.php:516)</p><pre>#0 
> D:\wamp\www\yi\framework\db\CDbCommand.php(411): 
> CDbCommand->queryInternal('fetchColumn', 0, Array) 

how can implement non-related table with cgridview search and sort options. 

My database table and result girdview

+0

當你將'$ criteria-> together'改爲'true'時出錯嗎? – dInGd0nG

+0

沒有錯誤,只是嘗試。我從下面得到了答案.. – bala

回答

10

我的問題我已經一步一步地發佈了關於此的博客文章。如果您遇到困難,請隨時閱讀並提出問題。這裏的鏈接不是在這裏寫下所有的東西而重複自己,這裏是鏈接:http://www.mrsoundless.com/post/2011/05/09/Searching-and-sorting-a-column-from-a-related-table-in-a-CGridView.aspx

它似乎幫助了一羣人。希望它也可以幫助你:)

+1

嗨,非常感謝,我從你的博客得到了答案。 ** $ criteria-> with = array('client','bposite'); ** 我從評論區獲得了它。我們需要添加單個數組中的所有關係。非常感謝你的幫助。 :) – bala

+0

你非常歡迎。 :) – MrSoundless

+0

輝煌。謝謝! –