How to add a column alias into yii active record model?

1.3k Views Asked by At

In my old project I used long id for table primary key, like 'shop_id' or 'order_id', but now I want to use just 'id' as alias for 'shop_id'. How to add this to the model class only, not changing tables. After that I'd like to have something like this

$model->id = $var

or

$var = $model->id 

and

$var = $model->attributes['id'].
1

There are 1 best solutions below

3
On

Simply create a getter method in your model code like this:

public function getId(){
   return $this->shop_id
}

If you have this on many tables, you could extend CActiveRecord and add a getter to that class like this:

public function getId(){
    return $this->{$this->tableSchema->primaryKey};
}