KVron
(Lfparedes)
April 29, 2015, 1:19am
1
Yeah, it’s me again
I have two tables:
[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:
[color="#000080 "]public function [/color]getDrugTypeb
[/b][b]{
[/b][indent][color="#000080 "]return [/color][color="#660000 "]$this[/color]->hasOne( DrugType::className b , [[/b][color="#008000 "]‘id’ [/color]=> [color="#008000 "]‘drug_type_id’ [/color][b]]);
[/b][/indent]}
[b]
[/b]
[color="#000080 "]function [/color]getDrugTypeLinkb {
[/b][indent][color="#000080 "]return [/color][color="#660000 "]$this[/color]->[color="#660e7a "]drugType [/color]->[color="#660e7a "]drug_type_name [/color][b];
[/b][/indent]}
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:
[color="#000080 "]public function [/color]actionIndexb {
[/b][indent][color="#660000 "]$dataProvider [/color]= [color="#000080 "]new [/color]ActiveDataProvider([ [/indent][indent][indent][color="#008000 "]‘query’ [/color]=> Drug::find b [/b]->orderBy( [color="#008000 "]‘drug_name ASC’ [/color]) ->joinWithb ,[/b][/indent]]); [/indent][b]
[/b][indent][color="#660000 "]$dataProvider[/color]->setSort[b]([
[/b][/indent][indent][indent][color="#008000 "]‘attributes’ [/color]=> [b][
[/b][/indent][/indent][indent][indent][color="#008000 "]‘drug_name’ [/color][b],
[/b][/indent][/indent][indent][indent][color="#008000 "]‘drugTypeLink’ [/color]=> [b][
[/b][/indent][/indent][indent][indent][color="#008000 "]‘asc’ [/color]=> [ [color="#008000 "]‘drug_type.drug_type_name’ [/color]=> [color="#660e7a "]SORT_ASC [/color][b]],
[/b][/indent][/indent][indent][indent][color="#008000 "]‘desc’ [/color]=> [ [color="#008000 "]‘drug_type.drug_type_name’ [/color]=> [color="#660e7a "]SORT_DESC [/color][b]],
[/b][/indent][/indent][indent][indent][color="#008000 "]‘label’ [/color]=> [color="#008000 "]‘Med Type’ [/color][b],
[/b][/indent][/indent][indent][indent][color="#008000 "]‘default’ [/color]=> [color="#660e7a "][b][i]SORT_ASC
[/i][/b][/color][/indent][/indent][indent][indent]], [/indent][/indent][indent]], [/indent][indent]]); [/indent][b]
[/b][b]
[/b][indent][color="#000080 "]return [/color][color="#660000 "]$this[/color]->render( [color="#008000 "]‘index’ [/color][b], [
[/b][/indent][indent][indent][color="#008000 "]‘dataProvider’ [/color]=> [color="#660000 "]$dataProvider[/color][b],
[/b][/indent][/indent][indent]]); [/indent]}
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?
KVron
(Lfparedes)
April 29, 2015, 1:33am
2
After looking at the logs, I found this:
[color="#333333 "]SELECT [/color]
[indent][color="#333333 "]drug
.* [/color][/indent][color="#333333 "]FROM [/color]
[indent][color="#333333 "]drug
[/color][/indent]
[color="#333333 "]LEFT JOIN [/color][indent]
[color="#333333 "]drug_type
ON drug
.drug_type_id
= drug_type
.id
[/color][/indent]
[color="#333333 "]ORDER BY [/color][indent]
[color="#333333 "]drug_name
, drug_type
.drug_type_name
[/color][/indent][color="#333333 "][size="2"]DESC LIMIT 20[/size][/color]
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]
Hi!
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:
http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/
Hope this helps.
Regards