Yii Framework Forum: Disambiguating Column Names In Stat Relation - Yii Framework Forum

Jump to content

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

Disambiguating Column Names In Stat Relation Its different than Yii Definitive Guide Rate Topic: -----

#1 User is offline   Shahzad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 16-March 12
  • Location:Islamabad, Pakistan

Posted 20 January 2013 - 01:45 PM

I have created a STAT relatin in my Agent model as following:

'hnp' => array(self::STAT, 'Request', 'agent_id', 'select'=>'SUM(hnp.total_due)'),


Where Agent has many Requests.

This throws an exception that hnp.total_due does not exists. But when i change it to t.total_due the results are ok.

This behavior is different than what is written in Yii's Definitive Guide, which is as following:-
In relational AR query, the alias name for the primary table is fixed as t, while the alias name for a relational table is the same as the corresponding relation name by default. For example, in the following statement, the alias name for Post and Comment is t and comments, respectively:

$posts=Post::model()->with('comments')->findAll();

Now assume both Post and Comment have a column called create_time indicating the creation time of a post or comment, and we would like to fetch posts together with their comments by ordering first the posts' creation time and then the comments' creation time. We need to disambiguate the create_time column like the following:
$posts=Post::model()->with('comments')->findAll(array(
    'order'=>'t.create_time, comments.create_time'
));


Can someone explain whether i am doing it wrong, or Yii Guide needs to be improved.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,044
  • Joined: 16-February 11
  • Location:Japan

Posted 20 January 2013 - 04:55 PM

Hi Shahzad,

The most important thing about STAT relation is, although it isn't mentioned in the guide AFAIK, that it is always loaded lazily. In other words, its related table isn't joined with the primary table when the main query is executed, and consequently we can't use a STAT relation for filtering or sorting.

Ah, yes, I agree with you that the guide should be improved.
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