access to a select_expr using AR attributes

Hi! This is my first post on the forum, I can’t speak English very well, so be patient :rolleyes:

In AR, how can I access to a select_expr using AR attributes, like I do with a real column field?

An dumb example.

The default scope of User Active Record is:




public function defaultScope()

    {

        return array(

            'select'=>"*, NOW() AS now", //ALL COLUMN AND A select_expr

        );

    }



I find all records, and want to access to a real column and a select_expr




$users=User::model()->findAll();

foreach ($users as $user) {

    print $user->username; //THIS WORK BECAUSE IS A REAL COLUMN

    print $user->now;      //THIS NOT WORK

}



I thought to use AR method like


public function getNow() {}

but how can I access to


now

field inside there?

Thanks!

P.S.: I now that I could get NOW() value from php, but this is only an example.

My real case is a bit more complex, but this is the essence.

you have to add public attribute in your model:




class User extends CActiveRecord {

  public $now;


  ...

}



Yii should fill this attribute with value from query you defined in defaultScope.

Thank you! That’s what I was looking for!

Btw, shouldn’t this be automated by Yii?

If I put something in the ‘select’ of the default scope, it’s obvious that I want to access to it in the istance of AR class.

not quite… you could use such column just in "heaving" expression for example…