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.
Page 1 of 1
Dynamic Getter/setter Methods Instead Of Properties
#2
Posted 11 January 2013 - 01:25 PM
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:
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>
"Less noise - more signal"
#3
Posted 11 January 2013 - 01:28 PM
Just overload it, and the rest of your team just use the property.
And probably wonder why $model->created now returns a Date object instead of a string.
And probably wonder why $model->created now returns a Date object instead of a string.
"Less noise - more signal"
#4
Posted 11 January 2013 - 01:33 PM
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"."
#5
Posted 11 January 2013 - 01:42 PM
Webnet668, on 11 January 2013 - 01:33 PM, said:
$carriet->getCreated();
It seems that you may have a typo there if you brought this from your code.
Rodrigo Coelho
Check my extension
giix: a code generator for Yii.
The complete beginner's study guide for the Yii Framework
Check my extension
giix: a code generator for Yii.

The complete beginner's study guide for the Yii Framework
#6
Posted 11 January 2013 - 01:59 PM
haha, of course. I didn't have a typo in my code, just in this forum post. I just did it again to double check and be sure that was the case.
#7
Posted 16 January 2013 - 11:12 AM
This won't work because the property "created" already exists
create getCreatedOn() and $carrier->createdOn will return the result of $carrier->getCreatedOn()
The CActiveRecord's magic __get() function will first check if an attribute exists else the value of $carrier->created wont be accessible any more.
create getCreatedOn() and $carrier->createdOn will return the result of $carrier->getCreatedOn()
The CActiveRecord's magic __get() function will first check if an attribute exists else the value of $carrier->created wont be accessible any more.
#8
Posted 16 January 2013 - 11:14 AM
In that case, I would think it would be better for Yii to store entity data within a $data property instead of just object properties as it is today.
#9
Posted 18 January 2013 - 12:01 PM
If 'created' is a column in your database table, you can't overload it with a getCreated() method.
However, what you should do, is create a method called getCreatedObject().
You can then access that method by using $model->createdObject;
However, what you should do, is create a method called getCreatedObject().
You can then access that method by using $model->createdObject;
Share this topic:
Page 1 of 1

Help

This topic is locked











