I love Yii, I think it’s a fantastic framework. When I made the move from Code Igniter I wanted to go back and redo previous projects in Yii because it was more fun to code with.
What I don’t understand is why Yii doesn’t utilize functions instead of properties when accessing model data/relations. I don’t believe it’s as overloading friendly as it could be. If I used $model->created I get a string which is a MySQL DateTime representation of a time. But say I wanted it to return a DateTime object? I’d have to go in and overload some functions which can quickly get messy. I’d much rather be able to just go in and overload $model->getCreated(); to do what i want it to do.
I find $model->getCreated(); both easier and cleaner to overload than $model->created.
Look in CComponent from which CModel and thus CActiveRecord is derived.
If you do, then you know that Yii uses __get and __set overloading.
So what you need to do, is overload getGreated in your model.
From the docs:
/**
* CComponent is the base class for all components.
*
* CComponent implements the protocol of defining, using properties and events.
*
* A property is defined by a getter method, and/or a setter method.
* Properties can be accessed in the way like accessing normal object members.
* Reading or writing a property will cause the invocation of the corresponding
* getter or setter method, e.g
* <pre>
* $a=$component->text; // equivalent to $a=$component->getText();
* $component->text='abc'; // equivalent to $component->setText('abc');
* </pre>
* The signatures of getter and setter methods are as follows,
* <pre>
* // getter, defines a readable property 'text'
* public function getText() { ... }
* // setter, defines a writable property 'text' with $value to be set to the property
* public function setText($value) { ... }
* </pre>
It doesn’t appear to be functional on attributes. In my application… $carrier->created returns a MySQL DateTime whereas $carriet->getCreated(); throws a … “do not have a method or closure named “getCreated”.”
If we have "CREATED_DATE" a mysql table column then how is it possible to achieve $model->setCreatedDate() and $model->setCreatedDate() function should work.
I do not want to create getter & setter methods for all the columns of a table. Mostly all the column names includes underscore & all are in CAPS.
In your case it is probably a good idea to create the actual method and a phpDoc block for it. Magic is not auto-completed unless you do the phpDoc. And cause you have to do the phpDoc anyway - why not write the getter you need?
Performance wise that is a good decision too - __get/__set/__call are not free you know…