dqg63264
2018-04-26 07:22
浏览 139
已采纳

Yii2:如何从searchModel回显参数

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 .

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题