Yii Framework Forum: Model class for static data - Yii Framework Forum

Jump to content

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

Model class for static data Rate Topic: -----

#1 User is offline   Giordano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 07-March 10

Posted 09 April 2010 - 05:55 AM

Hi all,

I'm wondering what would be the "best" approach for this:

I need a list of countries for a dropdownlist. For this I've created a model class derived from CFormModel which holds a public $countries array containing all countries.
This model is passed to the form where it can be used to populate a dropdownlist by simply using $countryMode->counties.

Would this be the yii way to go?
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 09 April 2010 - 06:04 AM

How are the countries managed and stored... if they are in a database then it's better to use CActiveRecord->findAll()...
if you read them from some txt file or code it manuali in the code than the array is OK
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 09 April 2010 - 06:50 AM

No need to use any CModel class (CFormModel) at all for this, as you'll never validate these countries. A simple custom class with static methods will do also.

MyHelper::countries();

0

#4 User is offline   Giordano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 07-March 10

Posted 09 April 2010 - 08:02 AM

Ok, thanks for that.

Is there a guideline on how to create one's own helpers or is it indeed just a custom class without any relation to yii's classes?
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 09 April 2010 - 08:19 AM

No guideline. Just put it somewhere, Yii can find it. E.g. protected/components.

Yii is a very open framework. It doesn't limit you in any way. So whatever works for you is fine. :)

Also check this:
http://www.yiiframew...oc/cookbook/45/
http://www.yiiframew...oc/cookbook/31/
0

#6 User is offline   Giordano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 07-March 10

Posted 09 April 2010 - 04:44 PM

Ok, thanks, I did create a static helper class, far simpler than my initial solution!
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