Yii Framework Forum: ambiguation, when? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

ambiguation, when? Rate Topic: -----

#1 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 28 May 2010 - 06:18 AM

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 :)
0

#2 User is offline   M Wotton 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 01-November 09

Posted 30 May 2010 - 11:04 PM

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.
Micha Wotton
Posted Image
0

#3 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 31 May 2010 - 09:47 AM

Okay thanks, but should everything be ambiguated?
0

#4 User is offline   M Wotton 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 01-November 09

Posted 31 May 2010 - 11:37 PM

View PostThomas Jensen, on 31 May 2010 - 09:47 AM, said:

Okay thanks, but should everything be ambiguated?

If in doubt, disambiguate. It is better to be clear than to get unexpected results.
Micha Wotton
Posted Image
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 01 June 2010 - 01:23 AM

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
0

#6 User is offline   Thomas Jensen 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 150
  • Joined: 01-August 09

Posted 01 June 2010 - 05:03 AM

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

#7 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 01 June 2010 - 06:52 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users