So I'm kinda new to Yii, and in my project, I needed to add and extra column containing data from another table in the database. I need to implement the filtering for this column too, and from what I've read, I need to add it to the search function in the model, but I can't seem to pull it off. Any help is appreciated. Thanks.
This is the view (the added column is 'areaReservada')
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'reserva-grid',
'dataProvider'=>$model->search(),
'itemsCssClass'=>"table table-striped",
'pager'=>array("htmlOptions"=>array("class"=>"pagination")),
//'ajaxupdate'=>false,
'columns'=>array(
array(
'name'=>'idreserva',
'header'=>'<p style="color: black">Número de reserva</p>',
//'value'=>'$data->area->nombre',
//'type'=>'raw',
'value'=>'$data->idreserva',
//'filter'=>Costoarea::getAreas(),
),
//'idreserva',
array(
'name'=>'fecha',
'header'=>'<p style="color: black">Fecha de Reserva</p>',
'value'=>'$data->fecha',
//'type'=>'raw',
//'value'=>'$model->estadoReserva_idestadoReserva',
//'filter'=>Costoarea::getAreas(),
),
//'fecha',
array(
'name'=>'areaReservada',
'header'=>'<p style="color: black">Área Reservada</p>',
'value'=>'$data->getArea($data->idreserva)',
//'type'=>'raw',
//'value'=>'$model->estadoReserva_idestadoReserva',
//'filter'=>Costoarea::getAreas(),
)));?>
This area my search function and the rules set in my model
public function search() { $criteria=new CDbCriteria; $criteria->compare('idreserva',$this->idreserva); $criteria->compare('fecha',$this->fecha,true); $criteria->compare('motivo',$this->motivo,true); $criteria->compare('monto',$this->monto); $criteria->compare('responsable',$this->responsable,true); $criteria->compare('estadoReserva_idestadoReserva',$this->estadoReserva_idestadoReserva); $criteria->compare('usuario_idusuario',$this->usuario_idusuario); return new CActiveDataProvider($this, array( 'criteria'=>$criteria,)); } public function rules() { return array(array('estadoReserva_idestadoReserva,usuario_idusuario', 'required'), array('estadoReserva_idestadoReserva, usuario_idusuario','numerical', 'integerOnly'=>true),array('monto','numerical'), array('motivo, responsable', 'length', 'max'=>45), array('fecha', 'safe'), array('idreserva, fecha, motivo, monto, responsable,estadoReserva_idestadoReserva, usuario_idusuario', 'safe',> 'on'=>'search'), ); }
To filter using new added extra column, you have to make following changes in your model as:
// For example, new extra column is 'areaReservada'.
public $areaReservada;
public function rules() {
return array(
array('areaReservada', 'safe'),
);
}
public function relations() {
return array(
'other_model' => array(self::BELONGS_TO, 'OtherModel', 'foreign_key'),
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->with = array('other_model');
$criteria->compare('idreserva', $this->idreserva);
$criteria->compare('fecha', $this->fecha, true);
$criteria->compare('motivo', $this->motivo, true);
$criteria->compare('monto', $this->monto);
$criteria->compare('responsable', $this->responsable, true);
$criteria->compare('estadoReserva_idestadoReserva', $this->estadoReserva_idestadoReserva);
$criteria->compare('usuario_idusuario', $this->usuario_idusuario);
$criteria->compare('other_model.column', $this->areaReservada); // Compare value contained in 'areaReservada' with column of other model to be filtered
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Hope this help you! Let me know if there is any concern from this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.