Bloodhound
Bloodhound

Reputation: 2966

Search in Gridview yii2

I have a table bus_route It has the following rules

public function rules()
    {
        return [
            [['schedule_id', 'stop_id'], 'required'],
            [['schedule_id', 'stop_id', 'route_order', 'is_destination'], 'integer'],
            [['departure_time'], 'safe']
        ];
    }

stop_id (foreign key) is the primary key (id) of table stops. Now i want to display the stop_name in stops table for the corresponding stop_id in bus route view. For that i added the following code in model

public function getStop()
    {
        return $this->hasOne(Stop::className(), ['id' => 'stop_id']);
    }

and in view

'stop.stop_name',

Its working but the search function is not working for the stop name field. Other fields show a box to search where stop_id field shows no box to search. How can i do a search for this field?

EDIT

BusrouteSearch.php

 public function search($params)
    {
        $query = BusRoute::find();
        $query->joinWith(['stop']);

        $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;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'schedule_id' => $this->schedule_id,
            'departure_time' => $this->departure_time,
            'stop_id' => $this->stop_id,
            'route_order' => $this->route_order,
            'is_destination' => $this->is_destination,
        ]);


        return $dataProvider;
    }

Upvotes: 3

Views: 608

Answers (1)

Sohel Ahmed Mesaniya
Sohel Ahmed Mesaniya

Reputation: 3450

Referring from here

In model Busroute.php

// ...
class Bus extends \yii\db\ActiveRecord
{
    // ...
    public function getStop()
    {
        return $this->hasOne(Stop::className(), ['id' => 'stop_id']);
    }

    public function getStop_name()
    {
        if(isset($this->stop->stop_name))
            return $this->stop->stop_name;
        else
            return '(stop name not set)';
    }
    // ...
}
// ...

In search model BusrouteSearch.php

class BusrouteSearch extends Busroute
{
    // ...
    public $stop_name;

    public function rules()
    {
        return [
            // other attributes
            [['stop_name'], 'safe'],
        ];
    }

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

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

        $dataProvider->setSort([
            'attributes' => [
                'id',
                // other attribute
                'stop_name' => [
                    'asc' => ['stops.stop_name' => SORT_ASC],
                    'desc' => ['stops.stop_name' => SORT_DESC],
                    'label' => 'Stop name',
                    'default' => SORT_ASC
                ],
                // other attribute
            ]
        ]);

        $query->joinWith(['stop']);
        $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;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'schedule_id' => $this->schedule_id,
            'departure_time' => $this->departure_time,            
            'route_order' => $this->route_order,
            'is_destination' => $this->is_destination,
        ]);

        $query->andFilterWhere(['like', 'stops.stop_name', $this->stop_name]);

        return $dataProvider;
    }


    // ...
}

In view file index.php (possibly @app/views/bus/index.php)

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        // other attributes
        'stop_name',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

Upvotes: 1

Related Questions