Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii CGridView sorting and searching with relation table

Tags:

yii

I have 3 tables in my database,

Image is below, I have followed http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

to get final grid with with search and sort purpose. so i have connected 2 tables in search() function like

  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',
                                    ),
                                    '*',
                                ),
                            ),
      ));
  }
}

But I am getting result for client, bposite seach and sort is showing problem like

>     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

like image 629
bala Avatar asked May 23 '12 18:05

bala


1 Answers

I've made a step by step blog post about this. Feel free to read it and ask questions if you're stuck. Instead of repeating myself by writing everything here too, here's the link: http://www.mrsoundless.com/post/2011/05/09/Searching-and-sorting-a-column-from-a-related-table-in-a-CGridView.aspx

It seems to have helped a bunch of people. Hope it helps you too :)

like image 168
MrSoundless Avatar answered Sep 19 '22 06:09

MrSoundless