How to transfer data for searching and sorting from several AR models to a CGridView widget? I don’t like to go down to the code, but ... basically the code is basically generated by the gii module:

$this->widget('zii.widgets.grid.CGridView', array( 'id'=>'ch-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>array( array( 'name' => 'title', 'header' => 'Заголовок', ), array( 'name' => 'category.title', 'header' => 'Категория', 'filter' => CHtml::listData(Category::model()->findAll(), 'id', 'title'), ), array( 'name' => 'chValue.title', 'header' => 'Величина' ), array( 'class'=>'CButtonColumn', ), ), 

))

the problem is that the widget didn’t care about searching and sorting in the fields selected using relationships (category.title). I do not understand how it works. please explain to me what i'm doing wrong and how to add a search for different models in one grid

    1 answer 1

    To sort in the model in the search function must be added.

     $criteria->with = array('category'); $criteria->together = true; return new CActiveDataProvider('Model', array( 'criteria'=>$criteria, 'sort'=>array( 'attributes'=>array( 'categorytitle'=>array( 'asc'=>'category.title', 'desc'=>'category.title DESC', ), ), ), )); 

    To search you will need to add a virtual field.

     private $_categorytitle = NULL; public function getCategorytitle() { if ($this->_categorytitle == NULL) $this->_categorytitle = $this->category->title; return $this->_categorytitle; } public function setCategorytitle($value) { $this->_categorytitle = $value; } 

    In the rules function in the array should be added

     array('categorytitle', 'safe') 

    In the widget view, replace category.title with categorytitle.

    • one
      Sorry, the error is out. Instead of: if ($ this -> _ categorytitle == NULL) $ this -> _ categorytitle = $ this-> category-> title; return $ this -> _ categorytitle; It is necessary: ​​if (($ this -> _ categorytitle == NULL) && ($ this-> category)) $ this-> _ categorytitle = $ this-> category-> title; return $ this -> _ categorytitle; - artkil
    • one
      In the first variant, the presence of the found category was not checked (i.e., this model corresponds to the found string from the database or this model is generated by the widget, and therefore is filled with data from a search query). In the second variant these cases are taken into account. - artkil
    • and not tell me more, how can I resolve the name conflict between category.title and title of the model itself? swears at this >> ambiguous column name: title and another question, can anything be done if there should be a lot of such pseudo-fields? Thanks for the help - heleg
    • Where exactly swears? If in the search function, then it is necessary in the line where the category name is compared to replace title with category.title. It should be: $ criteria-> compare ('category.title', $ this-> categorytitle, true); - artkil
    • >> where the category name is compared to replace title with category.title I don’t have a similar string at all >> $ criteria-> compare ('category.title', $ this-> categorytitle, true); I have not yet fully understood how dbcriteria works and therefore stupidly did your example. >> $ criteria-> with = array ('category'); $ criteria-> together = true; there must still be a line with compare? - heleg