Model "getter" methods

I love the dynamic __get() implementation in ActiveRecord that makes it possible to map db table column names to object properties. What I really love about it is the way CComponent’s __get() method makes it possible to create “dynamic” model attributes by defining getSomething() methods.

However, I’m now trying to create a “magic” get() method for an attribute that already represents one of my table’s columns (i.e. it is already in the model’s $_attributes array). So, the get() method I defined for this particular model never gets called because the first test in CActiveRecord::__get() (whether the single parameter passed to __get() exists in the $_attributes array) passes.

Is it possible to create a model method similar to the "magic" getSomething() methods, but for fields that do exist in the $_attributes array, so that in my view file, I can call $model->something to get the results of $model->getSomething(), just like I can do with fields that do not exists in the $_attributes array? If so, how is that done?

I had a little trouble articulating this. If it’s not clear, please ask me to clarify.

Thanks!

Tom

Extend CActiveRecord and override __get() (so that the check for the get-method comes first)?

Wow, that is so obvious, I feel a little silly for asking. ;)

Here is my implementation, in case anyone is interested:




	/**

	 * PHP getter magic method.

	 * This method is overridden here so that ActiveRecord will look first

         * for a custom getter() method in the model, THEN look for an attribute

         * (table column) if it doesn't find a custom getter().

	 * @param string property name

	 * @return mixed property value

	 * @see getAttribute

	 */

	public function __get($name)

	{

		$getter='get'.$name;

		if(method_exists($this,$getter))

			return $this->$getter();


	        return parent::__get($name);

	}