Yii Framework Forum: Hopefully an easy question about model attributes - Yii Framework Forum

Jump to content

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

Hopefully an easy question about model attributes Rate Topic: -----

#1 User is offline   kruege311 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-January 10

Posted 18 January 2010 - 08:05 PM

So I've been looking at the code trying to figure out how things work as I am just getting started with Yii version 1.1. The code I was looking at was the default code that is setup right after you initially set up Yii. Specifically I was playing around with the login page and was trying to figure out where the $attributes class variable is at that's used in the SiteController.php file. This was how I went about looking at things:

SiteController.php:
In actionLogin() I see $model=new LoginForm and then later I see $model->attributes=$_POST['LoginForm']. Seems simple enough, so on to my quest of finding the actual $attributes class variable. I went to look at LoginForm.php.

LoginForm.php:
I don't see any variable called $attributes. No biggie, I'll backtrack some more as this class extends CFormModel.

CFormMoel.php:
No variable called $attributes here either. But this extends another class called CModel, maybe it's there.

CModel.php:
Nope, don't see it here either. This extends CComponent, so let's check there.

CComponent.php
Well it's not here either and this class doesn't extend any others so we're at the top of the chain.

So anyway, you get my drift. Since I've already tried out the login page I know that the statement $model->attributes=$_POST['LoginForm']; does work, I just don't understand how it does when there doesn't seem to be any parent class that contains an $attributes variable when following the LoginForm's inheritance chain.


I'd appreciate any info, dumbed down or otherwise. Thanks in advance.
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 18 January 2010 - 09:44 PM

It's a public property defined by CModel.

http://www.yiiframew.../doc/api/CModel

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,898
  • Joined: 04-October 08
  • Location:DC, USA

Posted 18 January 2010 - 09:48 PM

Please also read the following guide careful. It forms the foundation of component-based programming:

http://www.yiiframew...asics.component
0

#4 User is offline   kruege311 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-January 10

Posted 18 January 2010 - 11:56 PM

Thanks for your replies guys. After reading your posts and and looking at your links, I had a better idea of what to search for online. I found that the __get and __set methods you are using are known as magic functions with php 5. Being that I was only familiar with php 4 this was definitely a new concept for me. It took me a little while but I finally found a good article online that broke it down in a way that made sense to me.

So in case there are any others that find __get and __set a little confusing and hard to follow, head on over to devshed.com and look for an article called Magic Functions in PHP 5. It's explained pretty well with an easy example using the old getters and setters methods and the new __get and __set methods. The article also mentions some pros and cons to using __get and __set versus the traditional getters and setters.

Once again, thanks for your replies.
2

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