Search code examples
yii2

yii2 gridcolumns filter text input to another model


how i can use 'filter' in $gridcolums with text input but referenced to different model with declared model in controller

here my code controller

public function actionList()
    {
        $searchModel = new WisudaSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('list', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

this my WisudaSearch()

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Wisuda;

/**
 * WisudaSearch represents the model behind the search form of `app\models\Wisuda`.
 */
class WisudaSearch extends Wisuda
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'nim', 'priode_id'], 'integer'],
            [['created_at', 'updated_at'], '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 = Wisuda::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

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

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'nim' => $this->nim,
            'priode_id' => $this->priode_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        return $dataProvider;
    }
}

this my Wisuda controller

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "wisuda".
 *
 * @property int $id
 * @property int $nim
 * @property int $priode_id
 * @property string $created_at
 * @property string $updated_at
 *
 * @property Angkatan $priode
 */
class Wisuda extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'wisuda';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['nim', 'priode_id'], 'required'],
            [['nim', 'priode_id'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['priode_id'], 'exist', 'skipOnError' => true, 'targetClass' => Angkatan::class, 'targetAttribute' => ['priode_id' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'nim' => 'Nim',
            'priode_id' => 'Priode ID',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * Gets query for [[Priode]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getPriode()
    {
        return $this->hasOne(Angkatan::class, ['id' => 'priode_id']);
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['nim' => 'nim']);
    }
}

and this my view

[
                        'attribute' => 'nama',
                        'label' => 'Nama',
                        'format' => 'raw',
                        'value' => function ($model) {
                            if (isset($model->user)) {
                                # code...
                                return $model->user->nama;
                            }
                            return '<span class="label label-warning">Belum Login</span>';
                        },
                    ],

how to add a filter in that gridcolumns


Solution

  • You need to take one public variable in model and define as safe and used that variable in grid with filter.