Yii Model problem, help!

Hi all expert here,

i am recently porting my application to Yii Framework, and i have a question about the models, situation as below:

i have a table name "company" which to store information about company e.g id, name, description .etc

but in my table i do append "company_" to each column, e.g. id => company_id, name => company_name

so for now, i use the model generated by "yiic shell", if i wan to get the company name, i have to do like $company->company_name, instead of $company->name

is there any method to set the the attribute "name" match to "company_name" in the database tables?

Thanks for you help !!

You can add this method to you model:



public function getName() {


    return $this->company_name;


}


Now you can access the name attribute just like any other: $company->name;

Beside this method?

is there any build in function like the attributeLables to customized so that we do not need to write a getter/setter function to match each column we wanted?

anyway, thanks you sluderitz

I don't think there is.

This is what you could do:



class MyActiveRecord extends CActiveRecord {


    public functinon __get($name) {


        $prefixedName = $this->columnPrefix . '_' . $name;


          if ($this->hasAttribute($prefixedName)) {


            return $this->getAttribute($prefixedName);


        } else {


            return parent::__get($name);


        }


    }


}


Then make your AR classes extend this one instead of CActiveRecord.  Then in each you set the $this->columnPrefix to whatever it is.

I havent tested this code, so it might have syntax or other errors, but this was just to give you the idea.

If you have any chance to just rename the fields, I'd recommend that though.