Related Search and AttributeLabels

Hello together :)

I’m a little confused about attributeLabels in a Search model.

I wanted to search related data (username instead of user_id) in a GridView.

So I followed this article:

http://www.ramirezcobos.com/2014/04/16/displaying-sorting-and-filtering-model-relations-on-a-gridview-yii2/

I did following changes to my code:

[spoiler]

app/models/EventSearch.php 



	// new public attribute for the username

	public $user; 


	// added in rules  

	public function rules()

	{

		return [

			...

			[['user'], 'string']

			[['user'], 'safe']

			...

		];

	}	


	// added to search 

	public function search($params)

	{

		...

		$query->joinWith(['user']);

		...

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

			'asc' =>    ['user.username' => SORT_ASC],

			'desc' =>   ['user.username' => SORT_DESC],

		];

		...

		$query->andFilterWhere(['like', 'user.username', $this->user]);

		...

	}

	

Then I changed the attribute in GridView like following: 



	[

		'attribute' => 'user', 

		'value'     => 'user.username'

	]

	

[/spoiler]

And I got the search for the username working…

Now to my question:

How to I set attributeLabels correctly for the created "user" attribute?

When I add the following in the Event Model (not the EventSearch Model!):




public function attributeLabels()

{

	return [

		...

		'user' => Yii::t('labels', 'User');

	];

}



The translation for the "new" attribute WORKS in GridView.

It also WORKS when I change the attributeLabels in the EvenSearch Model like following:




public function attributeLabels()

{

	return [

		'user'      => Yii::t('labels', 'User'),

	];

}



And the GridView like:




    [

        'label'     => $searchModel->getAttributeLabel('user'),

        'attribute' => 'user', 

        'value'     => 'user.username'

    ]



Okay. Good.

Now I tried the following in the EventSearch Model:




public function attributeLabels()

{

	return array_merge( 

		parent::attributeLabels(), 

		[

			'user'      => Yii::t('labels', 'User'),

		]

	);

}



And changed the view back to:




    [

        'attribute' => 'user', 

        'value'     => 'user.username'

    ]



Result: The translation for the "user" attribute does not work.

And I don’t understand why…

The Controller Action is like following:




        $searchModel = new EventSearch();

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


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

            'searchModel'   => $searchModel,

            'dataProvider'  => $dataProvider,

        ]);



Also the GridView uses $searchModel and $dataProvider…

So I thought GridView uses the attributeLabels from searchModel automatically?

So my questions are:

  • Do I missunderstand something?

  • or did I a mistake here?

  • What is the correct way / best practise for this?

Thank you for your replies. :)

Best Regards

Hi, from my experience. Your EventSearchModel which using CRUD genarator auto set $query = EventModel::find() in search() method. This will set yii to look for attributelabels of EventModel only.
To fix this, I set $query = EventSearchModel().find(). This will look for attributelabels from EventSearchModel which overrides the one in EventModel.

1 Like