2012-05-23 13 views
5

Tengo 3 tablas en mi base de datos,Yii CGridView clasificación y búsqueda con la tabla de relación

imagen está debajo, he seguido http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

para obtener rejilla final con la búsqueda y especie objetivo. así que he conectado 2 mesas en search() función como

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

Pero me estoy resultado para el cliente, seach bposite y especie está mostrando problema como

>  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

¿Hay algún error cuando cambias '$ criteria-> together' to' true'? – dInGd0nG

+0

Ningún error, solo intenté. recibí una respuesta de abajo ... – bala

Respuesta

10

I He hecho una publicación de blog paso a paso sobre esto. Siéntase libre de leerlo y hacer preguntas si está atrapado. En lugar de repetirme escribiendo todo aquí también, aquí está el enlace: http://www.mrsoundless.com/post/2011/05/09/Searching-and-sorting-a-column-from-a-related-table-in-a-CGridView.aspx

Parece que ha ayudado a un montón de gente. Espero que te ayude también :)

+1

Hola, muchas gracias, recibí una respuesta de tu blog. ** $ criteria-> with = array ('client', 'bposite'); ** Lo obtuve del área de comentarios. necesitamos agregar todas las relaciones en una sola matriz. Muchas gracias por tu gran ayuda. :) – bala

+0

Eres bienvenido. :) – MrSoundless

+0

Brillante. ¡Gracias! –

Cuestiones relacionadas