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 answers

3
Zack Newsham 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};
}