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 我的b站在没有碰到屏幕的情况下偶尔会自动跳出进度条,就像在屏幕上点了一下一样,但我并没有点。而且视频进度并没有变。这可能是什么原因造成的?
  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决