yii: accessing value in select as column

by prettyscripts on 2010-11-16 11:16

phpsqlyii

when selecting a column as alias name, a variable (property) of the same name must be manually defined in the model.

eg, sql:

Code:

select sum(column_1) as alias_1 from table_1 group by id;

in the model file:

PHP:

class MyModel as CActiveRecord {
    public $alias_1;
 
    function getAliasValue() {
        $criteria = new CDbCriteria();
        $criteria->select array('sum(column_1) as alias_1');
        $criteria->group 'id';
        $result MyModel::model()->find($criteria);
        return $result->alias_1;
    }
}

notes

line 2: define a new variable name. the name must be the same as the alias name used in select as clause in line 6.