after i render _search.php
in index.php
I have used Kartik DateRange to to get data between tow dates . here is my code :
<?= $form->field($model, 'date', [
'addon'=>['prepend'=>['content'=>'<i class="glyphicon glyphicon-calendar"></i>']],
'options'=>['class'=>'drp-container form-group']
])->widget(DateRangePicker::classname(), [
'useWithAddon'=>true
]);
?>
i used explode function to get the dates that i passed , and i get a real data , it works fine . my searchModel code :
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\patientservices;
class patientservicesSearch extends patientservices
{
public function rules()
{
return [
[['id', 'price'], 'integer'],
[['patient_id', 'doctor_id','service_id','date','state'], 'safe'],
];
}
public function scenarios()
{
return Model::scenarios();
}
public function search($params)
{
$query = patientservices::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => array('pageSize' => 150),
]);
$this->load($params);
if (!$this->validate()) {
if ( ! is_null($this->date) && strpos($this->date, ' - ') !== false ) {
list($start_date, $end_date) = explode(' - ', $this->date);
$query->andFilterWhere(['between', 'date', $start_date, $end_date]);
$this->date = null;
}
return $dataProvider;
}
$query->joinWith('patient');
$query->joinWith('service');
$query->joinWith('doctor');
$query->andFilterWhere([
'patient_services.id' => $this->id,
'patient_services.price' => $this->price,
]);
$query->andFilterWhere(['like','patient.patient_name',$this->patient_id]);
$query->orFilterWhere(['=','patient.patient_id',$this->patient_id]);
$query->andFilterWhere(['like','services.service_name',$this->service_id]);
$query->andFilterWhere(['like','doctors.doctor_name',$this->doctor_id]);
if ( ! is_null($this->date) && strpos($this->date, ' - ') !== false ) {
list($start_date, $end_date) = explode(' - ', $this->date);
$query->andFilterWhere(['between', 'date', $start_date, $end_date]);
$this->date = null;
}
$total = 0;
if (!empty($dataProvider->getModels())) {
foreach ($dataProvider->getModels() as $key => $val) {
$total += $val->price;
}
}
return $dataProvider;
}
}
after I submit the form passing the parameters to searchModel
and then as we know, then the date range become empty, The other fields do not become empty I need to echo the parameters that I passed to searchModel
Especially the date range. i don't know what is my problem here .
What looks like is that your date
value is not loading in the searchModel
otherwise it would show up in the field even if you manually assign the value to the field like below just before you call the view
. i am assuming the searchModel
name as ProductSearch
$params['ProductSearch']['date']='2018-05-20 - 2018-05-23';
$dataProvider=$model->search($params);
$this->render('search',['model'=>$model]);
so go to your model and add the date
field to your safe
rules along with the other listed
public function rules() {
return [
[ [ 'id' ] , 'integer' ] ,
[ [ 'some_field', 'some_other_field', 'date' ] , 'safe' ] ,
];
}
and if this field is not a part of the database table and just created for search purpose then you should declare this field inside your searchModel
as a public property of the class
public $date;
EDIT
After the addition of the search model it seems you are setting the date
field to null
so it is obvious that it would not show you once it searches successfully, so inside your search($params)
function at the end before you are calculating the $total
, remove the line $this->date=null;
from the condition
$query->andFilterWhere(['like','doctors.doctor_name',$this->doctor_id]);
if ( ! is_null($this->date) && strpos($this->date, ' - ') !== false ) {
list($start_date, $end_date) = explode(' - ', $this->date);
$query->andFilterWhere(['between', 'date', $start_date, $end_date]);
//-----> $this->date = null; //REMOVE THIS LINE //
}