如何自定义 Yii2 Gridview 搜索模型字段

How to customize Yii2 Gridview Search Model Fields

我已经使用 Gii 生成了 CRUD 模型、视图和控制器。

我正在使用的 table/model 用于抽奖结果,其中包含一个 id、一个 reset_at(日期时间)和一个 winner_id,这是一个用户的外键 table。该模型还有一种方法可以让所有通过连接点 table.

进入抽奖活动的用户

我正在使用 Kartik 的网格视图,我希望 reset_at 字段成为日期选择器,而 winner_id 成为获胜者的用户名。我很容易设置 table 行来显示格式化日期和获胜者的用户名。问题是顶部的搜索字段仍然是 datetime(字符串)和 winner_id(整数)。

我试图通过更改规则方法来调整搜索模型:

return [
    [['id', 'winner_id'], 'integer'],
    [['reset_at'], 'safe'],
];

return [
    [['reset_at', 'winner_id'], 'safe'],
];

我没有使用网格视图中的 id 列。然后我尝试将 winner_id 作为字符串读取,通过用户名等于字符串进行查找以获取用户,并在过滤中使用它的 id,如下所示:

// grid filtering conditions
$query->andFilterWhere([
    'id' => $this->id,
    'reset_at' => $this->reset_at,
    'winner_id' => $winner->id,
]);

我也尝试过与日期字符串类似的操作,我将过滤器调整为介于两者之间的日期时间,等等。但是当我这样做时,搜索字段从列的顶部消失。

如何自定义这些结果,甚至如何将完全自定义的属性添加到 gridview 以进行自定义过滤?

更新: 根据要求,这里是我的搜索模型(代码更改如下 this guide

<?php

namespace backend\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Raffle;

/**
 * RaffleSearch represents the model behind the search form of `common\models\Raffle`.
 */
class RaffleSearch extends Raffle
{
    public $winner;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'winner_id'], 'integer'],
            [['reset_at', 'winner'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Raffle::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        
        $dataProvider->sort->attributes['winner'] = [
            'asc' => ['app_user.username' => SORT_ASC],
            'desc' => ['app_user.username' => SORT_DESC],
        ];

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'reset_at' => $this->reset_at,
            'winner_id' => $this->winner_id,
        ])
        ->andFilterWhere(['like', 'app_user.username', $this->winner]);

        return $dataProvider;
    }
}

试试这个:

向您的查询添加联接 table:

public function search($params)
    {
        $query = Raffle::find();

        $query->joinWith(['USER_TABLE_RELATION_NAME']); //Change to you relation name
        ...
        $query->andFilterWhere(['like', 'USER_TABLE.name', $this->winner]);
        ...

It doesn't quite help with the date part unfortunately, I plan on using a between, but I'm very rusty with yii's querying. I'm not sure how to use that in active query

要在日期之间进行搜索,您可以使用以下命令:

$query->andFilterWhere(['BETWEEN', 'YOUR_DATE_TABLE_FIELD', $this->start_date, $this->end_date]);

作为其他选项的示例,请同时检查此答案:Search between two dates in yii2