Menus

Thursday, 1 February 2024

How to get relation value in index in yii2 and Filter


How to get relation value in index in yii2


Certainly! When using Yii2, the convention is to create a separate search model for handling search and filtering logic. Here's a basic example of a PostSearch model with filtering capabilities for the author.name attribute:

Assuming you have a Post model with a hasOne relation to Author:

  1. Create a PostSearch model:

// models/PostSearch.php

namespace app\models;

use yii\base\Model;

use yii\data\ActiveDataProvider;

class PostSearch extends Post

{

    public $authorName;

    public function rules()

    {

        return [

            [['id', 'author_id'], 'integer'],

            [['title', 'content', 'authorName'], 'safe'],

        ];

    }


    public function search($params)

    {

        $query = Post::find()->joinWith('author');

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

            'sort' => [

                'attributes' => [

                    'id',

                    'title',

                    'content',

                    'authorName' => [

                        'asc' => ['author.name' => SORT_ASC],

                        'desc' => ['author.name' => SORT_DESC],

                    ],

                ],

            ],

        ]);

        $dataProvider->sort->attributes['authorName'] = [

            'asc' => ['author.name' => SORT_ASC],

            'desc' => ['author.name' => SORT_DESC],

        ];

        $this->load($params);

        if (!$this->validate()) {

            return $dataProvider;

        }

        $query->andFilterWhere(['like', 'author.name', $this->authorName]);

        // Your other filtering logic goes here


        return $dataProvider;

    }

}


In your views/post/index.php, you can now use the GridView widget with the search model:

// views/post/index.php

use yii\grid\GridView;

// Other view code

<?= GridView::widget([

    'dataProvider' => $dataProvider,

    'filterModel' => $searchModel,

    'columns' => [

        // Your other columns


        [

            'attribute'=>'authorName'

            'value'=> 'author.name',

            'label' => 'Author Name',

        ],

        // Other columns

    ],

]); ?>


  1. In your Post model, ensure you have defined the relation to Author:
// models/Post.php

namespace app\models;

use yii\db\ActiveRecord;

class Post extends ActiveRecord
{
    // Your other attributes and methods here

    // Define the relation to Author

    public function getAuthor()
    {
        return $this->hasOne(Author::class, ['id' => 'author_id']);
    }

}


In your PostController, use the PostSearch model in the actionIndex method:

// controllers/PostController.php

namespace app\controllers;

use Yii;

use yii\web\Controller;

use app\models\PostSearch;

class PostController extends Controller

{

    public function actionIndex()

    {

        $searchModel = new PostSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        return $this->render('index', [

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }

}


 

No comments:

Post a Comment