Call search method to filter data


I try to code an application in Yii2 and don’t know how to call the “default search” method generated by Gii to filter data.


I have a table A (liedblock) with (id, title, …) and a table B (liedblocklieder) with (id, liedblock_id, …).

There is an foreign key relation between A and B with>B.liedblock_id.

Gii generated all Controller, Models and "SearchModels".

In my Liedblock.index-View I want to insert a link to the LiedblockliederController.index-Action using the foreigkey "liedblock_id".

liedblock/index.php (GrieView)


                'attribute' => 'title',

                'format' => 'raw',

                'value' => function ($data) {

                    return Html::a($data->title, ['liedblock-lieder/view', 'id'=> 0, 'liedblock_id' => $data->id]);


                'contentOptions' => ['style' => 'width:20px;'] // <-- right here


As result I expect to get all results from table B (liedblocklieder) wich match the given lieblocklieder_id.


  public function search($params) {

        $query = LiedblockLieder::find();

        $dataProvider = new ActiveDataProvider([

            'query' => $query,



        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;



            'id' => $this->id,

            'lied_id' => $this->lied_id,

            'liedblock_id' => $this->liedblock_id,

            'position' => $this->position,

            'create_time' => $this->create_time,

            'update_time' => $this->update_time,


        $query->andFilterWhere(['like', 'bemerkung', $this->bemerkung]);

        return $dataProvider;


Can somebody help me to create the correct link to fill in the attributes for the search() method please?


You can modify your index action of the LiedblockLieder controller to accept a liedblock_id:

public function actionIndex($liedblock_id = null)


    $searchModel = new LiedblockLiederSearch();

    if ($liedeblock_id !== null) {

        $searchModel->liedblock_id = $liedblock_id;

        // by this, LiedblockLiederSearch::search() will search all LiedblockLieder with specified liedblock_id


    ... (as usual) ...


By setting $searchModel->liedblock_id, LiedblockLiederSearch::search will return a data provider which will return the results that matches the given liedblock_id.

The view code of the liedblock/index should be:

    'attribute' => 'title',

    'format' => 'raw',

    'value' => function ($data) {

         return Html::a($data->title, ['liedblock-lieder/index', 'liedblock_id' => $data->id]);


The link should call the index action of the liedblockLieder controller with the specified liedblock_id.

No need to modify LiedblockLiederSearch.php.


It works. Thanks.