How we can access model attributes, without declaring them ?

In a model we are not declaring the attributes (i.e the name of the field of the table) then how we can refer to any field of table like $model->fieldName="some value" ?

Hi,

Because Yii is getting metadata from db (info about columns and relations) and using magic __get method to refer to either column or relation. I recommend you to grab the source and take a look.

Cheerio

model extends CActiveRecord.

Class CActiveRecord have a magic method




public function __get($name)

{

	if(isset($this->_attributes[$name]))

		return $this->_attributes[$name];

	else if(isset($this->getMetaData()->columns[$name]))

		return null;

	else if(isset($this->_related[$name]))

		return $this->_related[$name];

	else if(isset($this->getMetaData()->relations[$name]))

		return $this->getRelated($name);

	else

		return parent::__get($name);

}




Thats we can refer to any field