I have active record Notes and related Genres as $note->genre
Now i want to filter in gridview by $note->genre->name
I can assign filter value directly to $note->genre, making from object a string, but that’s not good i guess.
Tutorial http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/ doesn’t work, probably due to it’s out date.
my problem is next
Which means __setter cannot set. i understand that i can declare in model something like
public $filter_genre;
but i don’t want to do it, 'cause at some point i simply return whole class via jso_encode
here’s related parts of the code
//models/Notes.php
public function rules()
{
return [
.....
['genre filter_genre', 'safe', 'on' => 'filter']
];
}
//controllers/NotesController.php
public function actionGetNotes()
{
$filter_note = new Notes('filter');
$dp_criteria = new CDbCriteria(['with' => ['tempo','genre']]);
$dp_notes = new CActiveDataProvider('Notes');
$dp_notes->setCriteria($dp_criteria);
if (empty($_GET['Notes'])) {
.......
} else { //filter
$filter_note->attributes = $_GET['Notes'];
$dp_criteria->compare('note_id', $filter_note->note_id, true);
$dp_criteria->compare('title', $filter_note->title, true);
$dp_criteria->compare('genre.name', $filter_note->filter_genre, true); // not defined
$dp_criteria->compare('genre.name', $filter_note->genre, true); //WTF, but works if use genre name in list
}
$this->render('list', [
'dp_notes' => $dp_notes,
'filter_note' => $filter_note
]);
// views/notes/list.php
$this->widget('zii.widgets.grid.CGridView', [
'id' => 'notes-grid',
'dataProvider' => $dp_notes,
'filter' => $filter_note,
'columns' => [
.....
['name' => 'filter_genre', 'value' => '$data->genre->name'],
],
]);
thanks in advance