why was default alias for default table changed?

As in the lastest Version:

  • The alias name for the primary table in an AR query is fixed to be ‘t’

Why was this introduced?

My problem I have several Sql queries by hand and also build with the schema builder and now have to fix all sort orders and condition queries.

Which is not that easy and much work.

Why was this introduced and is there anything to set it back to the tablename itself?

I tried 1.1rc and had no problem with this, but after updating to the released 1.1 nothing works :S

Also i find it quite enoying that following works




$criteria=new CDbCriteria;

$criteria->condition=("`post`.`title` LIKE '%test%'");

$criteria->select =array('title','content');


// setting alias by hand to the same as in condition

$criteria->alias='post';


$params=array();

$result=Post::model()->findAll($criteria,$params);



creates this select




SELECT title, content FROM `tbl_post` `post` WHERE `post`.`title` LIKE '%test%'



but this doesn’t work (relation added)




$criteria=new CDbCriteria;

$criteria->condition=("`post`.`title` LIKE '%test%'");

$criteria->select =array('title','content');

$criteria->alias='post';


$params=array();

$result=Post::model()->with('author')->findAll($criteria,$params);



creates this select: (alias setting is ignored)




SELECT `t`.`title` AS `t0_c1`, `t`.`content` AS `t0_c2`, `t`.`id` AS `t0_c0`, `author`.`id` AS `t1_c0`, `author`.`username` AS `t1_c1`, `author`.`password` AS `t1_c2`, `author`.`salt` AS `t1_c3`, `author`.`email` AS `t1_c4`, `author`.`profile` AS `t1_c5` FROM `tbl_post` `t`  LEFT OUTER JOIN `tbl_user` `author` ON (`t`.`author_id`=`author`.`id`) WHERE (`post`.`title` LIKE '%test%')



of cause this would work:




$criteria=new CDbCriteria;

$criteria->condition=("`t`.`title` LIKE '%test%'");

$criteria->select =array('title','content');


$params=array();

$result=Post::model()->with('author')->findAll($criteria,$params);



This is just for showing a problem and is not code i use. I have several classes for query generation in ajax calls and export function which have the old ‘tablename’ as sort or search function and I really would like to make it work again without changing every name to ‘t.’

Also I find the readability isn’t as good as before.

regards Horizons

There were reasons to do it:

http://code.google.com/p/yii/issues/detail?id=796

http://code.google.com/p/yii/issues/detail?id=817

And a new proposal that will solve your problem:

http://code.google.com/p/yii/issues/detail?id=818

the proposal doesn’t work see demo above (used the lastest blog demo)

with




$criteria=new CDbCriteria;

$criteria->condition=("`post`.`title` LIKE '%test%'");

$criteria->select =array('title','content');


$params=array();

$result=Post::model()->with('author')->findAll($criteria,$params);



also added

function alias(){

    return "post";

}

to the post model still doesn’t work when relations are used.

regards Horizons

It’s a proposal, so it’s not yet implemented or even scheduled to be implemented.