I have used a from_date
& to_date
attributes to search my data and have placed it in the Model as safe attributes. Now I want to display the same from_date
and to_date
in the CGridview with other data from the model.
Shouldn't I just be able to use $data->from_date
in my CGridView?
Model
public $from_date;
public $to_date;
public function rules() {
array('from_date, to_date', 'safe', 'on'=>'search'),
}
public function search(){
//....
if(!empty($this->to_date) && !empty($this->from_date))
{
$criteria->addCondition("date($created_date) >= '$this->from_date' and date($created_date) <= '$this->to_date'");
}
else if(!empty($this->from_date) && empty($this->to_date))
{
$criteria->addCondition("date($created_date) >= '$this->from_date'");
}
else if(!empty($this->to_date) && empty($this->from_date))
{
$criteria->addCondition("date($created_date) <= '$this->to_date'");
}
//....
}
Controller
$model = new Tickets('search');
if (!empty($_GET)) {
$model->ticket_id = isset($_GET['ticket_id']) ? $_GET['ticket_id'] : '';
$model->from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
$model->to_date = isset($_GET['to_date']) ? $_GET['to_date'] : '';
}
$this->render('search', array(
'model' => $model
));
View
$this->widget('bootstrap.widgets.TbGridView', array(
'type' => 'striped bordered condensed',
'dataProvider' => $model->search(),
'columns' => array(
array(
'name' => 'From date',
'type' => 'html',
'value' => '$data->from_date',
),
),
));
Try the following adjustments. I don't see where you specified how the $_GET
attributes were being set, so I included the standard way of doing it with CGridView
. Let me know if it works.
Controller:
$model = new Tickets('search');
//remove any default values
$model->unsetAttributes();
//set the attributes based on the standard syntax of how CGridview populates GET
if (!empty($_GET['Tickets'])) {
$model->attributes = $_GET['Tickets'];
}
$this->render('search', array(
'model' => $model
));
Model: add ticket_id
to rules, to automatically process set it if present.
public function rules() {
array('from_date, to_date, ticket_id', 'safe', 'on'=>'search'),
}
View:
$this->widget('bootstrap.widgets.TbGridView', array(
'type' => 'striped bordered condensed',
'dataProvider' => $model->search(),
'filter'=>$model,//should provide default filtering
'columns' => array(
array(
'name' => 'From date',
'type' => 'html',
'value' => '$data->from_date',
),
),
));
Side note: Your model's search
method had huge SQL injection vulnerabilities, but let's solve one problem at a time first.