Disambiguating Column Names In Stat Relation

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.

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.