ActiveDataProvider won't sort

Yeah, it’s me again

I have two tables:

  • [font="Courier New"]drug[/font]

  • [font="Courier New"]drug_type[/font]

[font="Courier New"]drug[/font] has [font="Courier New"]drug_type_id[/font] as a FK.

In my [font="Courier New"]Drug[/font] model I have:

Also added [size=“2”][color="#008000"][font=“Menlo”]‘drugTypeLink’ [/font][/color][font=“Menlo”]=> [/font][color="#008000"][font=“Menlo”]‘Med Type’[/font][/color][/size][size=“2”] to [font=“Courier New”]attributeLabels()[/font].[/size]

In my [font="Courier New"]DrugController[/font] I have:

Data appears as expected in the gridView at [font="Courier New"]/views/drug/index.php[/font] The only problem is that [font="Courier New"]drugTypeLink[/font][size="2"] never sorts.[/size]

[size=“2”]I’m not searching data, I just want to show the grid with drug names and drug types and be able to sort by both columns.[/size]

What am I doing wrong now?

After looking at the logs, I found this:

ActiveDataProvider is sorting by both columns, [color="#333333"][size="2"][font="Courier New"]drug_name, drug_type.drug_type_name[/font]. [/size][/color]

[color="#333333"][size="2"]How do I instruct [/size][/color][size="2"]ActiveDataProvider to [/size][size="2"]use only the one I clicked on?[/size]


Lets say for exmaple you have a related user table and a relation "getUser()" …

Then you can add it in the search function like:

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

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

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


Take a look at this - that really helped me to get related search / filtering working:

Hope this helps.