[Solved] Is It Safe To Use Table Alias Assigned By Yii In Subquery ?

Good Morning,

I am new to Yii and enjoy it so far, but i got a question :)

I have a table tmenu, with columns mnuid, description, path, parent, mnuorder.

In this table there is a two level menu where the parent field is nullable, if null it mean this row have no parent or if numeric it’s a valid mnuid.

I wrote the code below to order the menu admin CGridView like this :

In first level mnuorder (parent IS NULL) but with all children (second level) after they parents in they own menu order


$dataProvider->sort->defaultOrder='IFNULL((SELECT mnuorder FROM tmenu b WHERE b.mnuid = t.parent), mnuorder), parent';

This code work correctly ordering my menu and simulating the two level "tree view" it should manage.

But because the order clause use a subquery, which is linked to the main query table, i have to guess that Yii is using "t" as a table alias (thanks to the error message).

Now my questions are: Is it safe to use "t.parent" in the subquery knowing this "t" alias was assigned by Yii ? Or maybe there is a safer way to do the same whithout guessing t is the alias of the main table ?

Thanks in advance and regards,

Jean

PS: find attached a screenshot of the result

U are safe to use ‘t’ cause it’s default table alias. I didn’t have problems using it. But, sure U can change table alias, please check:

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#setTableAlias-detail

and also U can get table alias:

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getTableAlias-detail

1 Like

Thanks for the help,

I tried it and succed with the code :


$dataProvider->model->setTableAlias('m');