Yii Framework Forum: How To Add Dynamic Attributes To Model And Get Them In Findall() Database Query - Yii Framework Forum

Jump to content

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

How To Add Dynamic Attributes To Model And Get Them In Findall() Database Query How to add dynamic attributes to model and get them in findall() datab Rate Topic: -----

#1 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 18 December 2012 - 12:08 AM

Hi,
I want to add dynamic attributes to a model. Say my table has 3 fields(id,groupname,ownerId).I have added one more property to the model named isOwner.This property is not in th model table.
After i fetch all records related to the user(say userID =1), i have to attach one more property isOwner.This property should be 1 if the user is the owner of group and 0 if he is not.
I declared a public property, public $isOwner to my model class. I created a function to find if user is owner.Now i want isOwner property to be 1 for every record i fetch using findAll() function. I used the afterFind() function also but its not working.?
Any ideas?

This is in continuation to the topic
http://www.yiiframew...__1#entry185700

Thanks to all
0

#2 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 18 December 2012 - 08:17 AM

Any ideas guys?
0

#3 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 18 December 2012 - 10:39 AM

I'm not sure, but maybe a stat relation in the model?
0

#4 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 19 December 2012 - 04:39 AM

Thanks Ragua,
What does that mean?

Regards
0

#5 User is offline   smith19 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 26-November 12

Posted 20 December 2012 - 02:42 AM

Hi Friends, Finally i found out. It is the problem with the CJSON::encode problem. this method only encodes the properties of the model. It doesn't encode any relational or dynamic property. SO i wrote my own function and it works.!

Thanks to all
Smith
0

#6 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 20 December 2012 - 05:07 AM

Glad you solve your problem. For free, a stat relation allow you to have a calculated field non-database (agregated) in your model. You can look at:
http://www.yiiframew...ms-in-cgridview

View Postsmith19, on 19 December 2012 - 04:39 AM, said:

Thanks Ragua,
What does that mean?

Regards

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