Yii2:如何从searchModel回显参数

时间:2018-04-26 07:22:02

标签: php parameters yii2 yii2-advanced-app

<form method="POST"> <fieldset> <div class="form-group"> @Html.TextBoxFor(m => m.subid, new { @class = "form-control", @placeholder = "Email" }) > </div> <button href="" class="btn btn-gm btn-success">View</button> </fieldset>

中呈现_search.php之后

我使用Kartik DateRange来获取两个日期之间的数据。 这是我的代码:

index.php

我使用explode函数来获取我通过的日期,并且我得到一个真实的数据,它工作正常。 我的searchModel代码:

<?= $form->field($model, 'date', [
    'addon'=>['prepend'=>['content'=>'<i class="glyphicon glyphicon-calendar"></i>']],
    'options'=>['class'=>'drp-container form-group']
        ])->widget(DateRangePicker::classname(), [
            'useWithAddon'=>true
        ]);
 ?>

我提交表单后将参数传递给<?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; } } 然后我们知道,那么日期范围变为空,其他字段不会变空我需要回显我传递给{{1的参数特别是日期范围。我不知道这里有什么问题。

1 个答案:

答案 0 :(得分:1)

看起来是date值没有加载到searchModel,否则它会显示在字段中,即使您在调用之前手动将值分配给字段,如下所示view。我假设searchModel名称为ProductSearch

$params['ProductSearch']['date']='2018-05-20 - 2018-05-23';
$dataProvider=$model->search($params);
$this->render('search',['model'=>$model]);

所以转到您的模型并将date字段与其他列出的<{1}}规则一起添加

safe

如果此字段不是数据库表的一部分并且仅为搜索目的而创建,那么您应该在public function rules() { return [ [ [ 'id' ] , 'integer' ] , [ [ 'some_field', 'some_other_field', 'date' ] , 'safe' ] , ]; } 内将此字段声明为该类的公共属性

searchModel

public $date;

添加搜索模型后,您似乎将EDIT字段设置为date,因此很明显,一旦成功搜索,它就不会显示给您,因此在{{1}内部在计算null之前结束函数,从条件中删除行search($params)

$total