Yii CGridView使用关系表进行排序和搜索

bal*_*ala 5 yii

我的数据库中有3个表,

图片如下,我已经关注http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

以搜索和排序为目的获得最终网格.所以我在search()功能上连接了2个表

  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',
                                    ),
                                    '*',
                                ),
                            ),
      ));
  }
}
Run Code Online (Sandbox Code Playgroud)

但我得到客户端的结果,bposite seach和sort显示问题就像

>     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.
Run Code Online (Sandbox Code Playgroud)

我的数据库表和结果girdview

MrS*_*ess 10

我已经做了一篇关于此的博客文章.如果你遇到困难,请随意阅读并提出问题.这里的链接不是通过在这里写一切来重复自己,而是链接:http://www.mrsoundless.com/post/2011/05/09/Searching-and-sorting-a-column-from-a-related-table- IN-A-CGridView.aspx

它似乎帮了很多人.希望它也可以帮助你:)