Table column names to ActiveRecord instance attributes' configurable mapping

For example:

I have a table with columns: col_a, col_b, col_c

I’d like to map it to the active record $obj’s attribte: attr_a, attr_b, attr_c

So col_a, col_b, col_c’s value could be access and update via $obj->attr_a, $obj->attr_b, $obj->attr_c

Is there any mapping function in active record class for this purpose?

Prototype:

public function columnMapping(){

return array(


  'col_1' => 'attr_a',


  ...,


);

}

I don’t think there’s any mapping function for this, but you could take advantage of Yii’s set and get magic methods.




function getAttr_a() {

  return $this->col_a;

}

function setAttr_a($value) {

  $this->col_a = $value;

}

Unfortunately, you would need to code both for each mapped attribute.