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

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条回答 默认 最新

  • dongzhi5587 2018-04-26 23:10
    关注

    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 //
    
      }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么