简体   繁体   English

如何在 yii2 型号上添加过滤器

[英]How do I add a filter on yii2 models

I have a query used to read data from a view.我有一个用于从视图中读取数据的查询。 I am then applying a non related sql filter to this, to take out the models I don't need and then returning an arrayDataProvider instance.然后我对此应用一个不相关的 sql 过滤器,取出我不需要的模型,然后返回一个 arrayDataProvider 实例。 The filter I am applying is the promotion period.我应用的过滤器是促销期。 However, when I pass query params to this search model, it doesn't work.但是,当我将查询参数传递给此搜索 model 时,它不起作用。 How do I solve this?我该如何解决这个问题?

Here's the code to select data from the view:这是视图中 select 数据的代码:

public function search($params, $promotion_period)
{
    $query = StaffEmploymentListView::find()->alias('LIST')
            ->joinWith(['employment EMPLOYMENT'])
            ->where(['LIST.EMPLOYMENT_TYPE' => 'PROMOTION']);
   
    $query->andFilterWhere(['like', 'DEPT_NAME', $this->DEPT_NAME])
            ->andFilterWhere(['like', 'COL_NAME', $this->COL_NAME])
            ->andFilterWhere(['like', 'GRADE_DESCR', $this->GRADE_DESCR])
            ->andFilterWhere(['like', 'DESG_NAME', $this->DESG_NAME])
            ->andFilterWhere(['like', 'TRIBE_NAME', $this->TRIBE_NAME])
            ->andFilterWhere(['like', 'GENDER', $this->GENDER])
            ->andFilterWhere(['like', 'HOME_DISTRICT', $this->HOME_DISTRICT]);

    $allModels = [];

    if($promotion_period !== null){
        $to_date = strtotime($promotion_period['to_date']);
        $from_date = strtotime($promotion_period['from_date']);
        foreach ($query->all() as $model)
        {
            $effective_date = strtotime($model->employment->APPOINT_DATE);
            if ($effective_date >= $from_date && $effective_date <= $to_date){
                $allModels[] = $model;
            }
        }
    }
    else{
        foreach ($query->all() as $model)
        {
            $allModels[] = $model;
        }
    }

    $dataProvider = new ArrayDataProvider([
        'allModels' => $allModels,
        'pagination' => [
            'pageSize' => 20,
        ],
        'sort' => false
    ]);

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    
    return $dataProvider;
}

Here's the dump of the params from the URL:这是来自 URL 的参数转储:

[
'from_date' => ''
'to_date' => ''
'PromotedStaffSearch' => [
    'COL_NAME' => 'CENTRAL ADMINISTRATION'
    'DEPT_NAME' => ''
    'GRADE_DESCR' => ''
    'DESG_NAME' => ''
    'GENDER' => ''
    'TRIBE_NAME' => ''
    'HOME_DISTRICT' => ''
]

] ]

Like i understand, you trying to find all models between specified period if it is set.就像我理解的那样,如果已设置,您将尝试查找指定时间段between的所有模型。

Try this:尝试这个:

public function search($params, $promotion_period)
{
    $query = StaffEmploymentListView::find()->alias('LIST')

            ->joinWith(['employment']) //Here should be your relation name from StaffEmploymentListView model 

            ->where(['LIST.EMPLOYMENT_TYPE' => 'PROMOTION']);
   
    

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
            'pageSize' => 20,
        ],
        'sort' => false
    ]);

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    $query->andFilterWhere(['like', 'DEPT_NAME', $this->DEPT_NAME])
            ->andFilterWhere(['like', 'COL_NAME', $this->COL_NAME])
            ->andFilterWhere(['like', 'GRADE_DESCR', $this->GRADE_DESCR])
            ->andFilterWhere(['like', 'DESG_NAME', $this->DESG_NAME])
            ->andFilterWhere(['like', 'TRIBE_NAME', $this->TRIBE_NAME])
            ->andFilterWhere(['like', 'GENDER', $this->GENDER])
            ->andFilterWhere(['like', 'HOME_DISTRICT', $this->HOME_DISTRICT]);

    //$allModels = [];

    if(is_array($promotion_period) && isset($promotion_period['to_date']) && isset($promotion_period['from_date'])){
        //also check if you $promotion_period params are not empty
        $to_date = strtotime($promotion_period['to_date']);
        $from_date = strtotime($promotion_period['from_date']);

        $query->andFilterWhere(['BETWEEN', 'EMPLOYMENT_TABLE_NAME.APPOINT_DATE', $from_date, $to_date]);

    }
    
    //Now you can get all your filtered models as array like this:
    return $dataProvider->getModels();
}

Documentation for Data Providers 数据提供者的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM