Question About Models And Cactiverecord

Hi guys, I just want to know where are the properties of the model defined, because I see $this->username for example, but I don’t know where it is defined.

Maybe you could give me a link where this is explained.


This depends on the type of model that you’re using. If you’re using an ActiveRecord model, the property (or attribute) is defined by your database schema (model’s table -> column = property).

In that case your model class has to extend from CActiveRecord. If you’re using a regular model (like CFormModel or CModel), you have to define these attributes in your code.

In CActiveRecord the properties are actually held in the CActiveRecord class’ private $_attribute property and this property is modified via php automagic class methods __get() and __set()

It’s not so easy to explain this completely, but you could take a look at the following pages:

TIP: click on the "show" link next to "source:" for details in the Yii Class documentation, I usually find the source exceptionally readable and generally helps you understand how stuff works in Yii!

  • CActiveRecord::__get()

  • CActiveRecord::__set()

  • CActiveRecord::getAttributes()

  • PHP Overloading