Yii Framework Forum: Dynamic Getter/setter Methods Instead Of Properties - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dynamic Getter/setter Methods Instead Of Properties

#1 User is offline   Webnet668 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 23-October 12

Posted 11 January 2013 - 12:22 PM

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.
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

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:


/**
 * 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"
0

#3 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

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. :P
"Less noise - more signal"
0

#4 User is offline   Webnet668 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 23-October 12

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"."
0

#5 User is offline   Rodrigo Coelho 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 665
  • Joined: 05-August 10
  • Location:Rio de Janeiro, Brazil

Posted 11 January 2013 - 01:42 PM

View PostWebnet668, 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.
0

#6 User is offline   Webnet668 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 23-October 12

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.
0

#7 User is offline   MichaelH 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 09-February 10

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.
0

#8 User is offline   Webnet668 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 23-October 12

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.
0

#9 User is offline   yJeroen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 06-September 11
  • Location:The Netherlands

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;
0

#10 User is offline   onkar lal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 13-September 11
  • Location:India

Posted 11 November 2013 - 01:49 AM

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.
0

#11 User is offline   Psih 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 114
  • Joined: 30-June 10

Posted 11 November 2013 - 09:19 AM

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...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users