I have a database model like this:
I’ve created models (Media
, MediaSubformat
, MediaFormat
), search models, controllers and views for each table with Gii
. Without any changes, the index.php
view looks like this:
(Tell a lie, I changed the attributeLabels). Next, I’ve edited MediaSearch.php
and changed the rules()
function to:
public function rules(): array
{
return [
[['media_id'], 'integer'],
[['media_title', 'media_subformat_id', 'media_wikipedia_url', 'media_note'], 'safe'],
];
}
And in the same file, edited $query->andFilterWhere
, so that it’s as follows:
// grid filtering conditions
$query->andFilterWhere([
'media_id' => $this->media_id
]);
$query->andFilterWhere(['ilike', 'media_title', $this->media_title])
->andFilterWhere(['ilike', 'media_wikipedia_url', $this->media_wikipedia_url])
->andFilterWhere(['ilike', 'media_note', $this->media_note])
->andFilterWhere(['ilike', 'media_subformat_name', $this->media_subformat_id]);
And the GridView::widget
, columns
element in media/index.php
, like this:
'columns' => [
'media_id',
'media_title',
[
'attribute' => 'media_subformat_id',
'value' => 'mediaSubformat.media_subformat_name',
'label' => 'Subformat',
],
'media_wikipedia_url:url',
'media_note:ntext',
[
'class' => ActionColumn::class,
'urlCreator' => function ($action, Media $model, $key, $index, $column) {
return Url::toRoute([$action, 'media_id' => $model->media_id]);
}
],
],
Which results in the following:
Finally, I added a new relation to Media.php
:
/**
* Gets query for [[MediaFormat]].
*
* @return ActiveQuery
*/
public function getMediaFormat(): ActiveQuery
{
return $this->hasOne(MediaFormat::class, ['media_format_id' => 'media_format_id'])
->via('mediaSubformat');
}
And inserted the following into the column element for the GridView::Widget above:
[
'attribute' => 'mediaFormat.media_format_id',
'value' => 'mediaFormat.media_format_name',
'label' => 'Format',
],
And the view now looks like this:
I cannot find the information I need to add sort and filter for media_format_id
/media_format_name
. I’d be really grateful for some pointers.
Many thanks,
Nic