usually CGridView with default filter (one model at one table) like this :
in controller :
$model=new model_A('search');
$this->render('rendering_file',array(
'datacall'=>$model,
));
in view :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'data-logs',
'dataProvider'=>$datacall->search(),
'filter'=>$datacall,
'columns'=>array(
...
...
if model_A has criteria, then become :
in controller : (with CDbCriteria)
$model=new model_A('search');
$merge=new CDbCriteria;
if(!Yii::app()->request->isAjaxRequest)
$merge->order = 'some_field ASC';
if(isset($_GET['model_A']))
{
$model->attributes=$_GET['model_A'];
$merge->order = 'some_field ASC'; //just customize this after filtering
}
$this->render('rendering_file',array(
'merge'=>$merge,
'datacall'=>$model,
));
in view :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'data-logs',
'dataProvider'=>$datacall->search($merge),
'filter'=>$datacall,
'columns'=>array(
...
...
with some additional data on model_A :
public function search($merge=null) //add $merge == null
{
$criteria=new CDbCriteria;
$criteria->compare('no',$this->no);
...bla bla
if($merge!==null) //add this line
$criteria->mergeWith($merge); //add this line
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
[size="6"]And my question :[/size]
how send dataProvider on CGridView on the view file
if i have some joining tables
(e.q. : model_A represent table A, model_B represent table B )
i wanna search on table B where i have to join with table A on field e.q. ‘student_id’
i make :
in controller :
$model=new model_B('search');
$merge=new CDbCriteria;
if(!Yii::app()->request->isAjaxRequest)
{
$merge->join = 'table_A ta ON ta.student_id=t.student_id';
$merge->order = 'some_field ASC';
}
if(isset($_GET['model_B']))
{
$model->attributes=$_GET['model_B'];
$merge->join = 'table_A ta ON ta.student_id=t.student_id';
$merge->order = 'some_field ASC'; //just customize this after filtering
}
$this->render('rendering_file',array(
'merge'=>$merge,
'datacall'=>$model,
));
in view :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'data-logs',
'dataProvider'=>$datacall->search($merge),
'filter'=>$datacall,
'columns'=>array(
...
array(
'name' => 'some_field_on_model_A',
'value' => '$data->relation_to_model_A->some_field_on_model_A', //it occurs error
),
with some additional data on model_B :
public function relations()
{
return array(
'relation_to_model_A' => array(self::BELONGS_TO, 'table_A', 'student_id'),
'relation_to_model_C' => array(self::BELONGS_TO, 'table_B', 'period_id'),
);
}
public function search($merge=null) //add $merge == null
{
$criteria=new CDbCriteria;
$criteria->compare('no',$this->no);
...bla bla
if($merge!==null) //add this line
$criteria->mergeWith($merge); //add this line
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
the problem :
-
i couldnt search on filter, just loading and finish, not filtering my input
-
i couldnt print column with data on another table (field on table A)
or
could you show me some tutorial how using CGridView filtering when models are joining with CDbCriteria?
or using another db classes?
thanks
jefriyanto