1

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'].
raina77ow
  • 103,633
  • 15
  • 192
  • 229
Alex Shwarc
  • 822
  • 10
  • 20

1 Answers1

0

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};
}
Zack Newsham
  • 2,810
  • 1
  • 23
  • 43
  • No, I did this, of course. But in this case we can just get or set data - $model->alias_column. But !! what about find() methods? I see we have to overload some CActiveFider methods /related to schema/ – Alex Shwarc Sep 09 '13 at 09:52
  • Ahh, I see - that isn't the question you asked however, you asked how to use a non table variable in this form: `$var = $model->id`. You would have to overload quite a lot of methods in Yii to accomplish this, as there isn't a single place that builds queries - it is quite a convoluted process. – Zack Newsham Sep 11 '13 at 17:46
  • Thanks, I see that the best way is to use PostgreSQL database capabilities (rule system). – Alex Shwarc Sep 14 '13 at 12:57