ambiguation, when?

Hi all!

I’m very confused about when to do so and when not to…

Is there some common rule i can use or? An example, from an ARbehavior:




$this->Owner->getDbCriteria()->mergeWith(array(

				'order' => 'COUNT(visits.id) DESC',

				'join' => ('LEFT JOIN visits ON (visits.itemId = t.id AND visits.itemName = :tableName)'),

				'params' => array(':tableName' => $this->Owner->tableName()),

				'limit' => $limit,

				'group' => 't.id',

				'select' => 't.*'

			));



Should these be disambiguated? How can i assure that t.id will always means ownertable.id?

Thanks :)

I may be wrong, but as I understand it, ‘t’ in this context always refers to the table specified in the model you are using. Since you are using the ‘Owner’ model, ‘t’ will refer to the table specified in the Owner model class.

Okay thanks, but should everything be ambiguated?

If in doubt, disambiguate. It is better to be clear than to get unexpected results.

You should disambiguate when there are fields with the same name in different tables.

If, for example, in table visits there is a field id and in the main table too, you should disambuguate id.

You can also customize the alias ‘t’ with the property alias of CDbCriteria

Okay, but will Yii always ambiguate the fields, before sending the SQL? What if i have a field named "count"?

Yii is not disambiguatin, is up to you.

When you have to use a field in WHERE, HAVING, GROUP BY you have to be sure that field you use are disambiguated.

If you have an exception like ‘field id in GROUP BY clause is ambiguous’ means that you should use t.id instead of id.

For aggregated field (count, sum) I am using alias by passing in CDbCriteria->select smth like ‘count(*) as total’.

Like that in HAVING clause I can use total.