dropDownList and active record

Hi everyone,

I am new to Yii and MVC but I am trying to figure something out. I have a problem with a dropDownList and using an CActiveRecord model. I have been following the Blog tutorial.

The sql table is like this:


| id | name |


| 1 | Admin |

| 2 | Moderator |


The model is in the base of the application while the view _form.php is under a module called admin.

By using the model attached, and the code below:


<?php echo $form->dropDownList($model, 'idUserGroup', UserGroup::getGroups()); ?>

My dropDownList is empty.

When I use the following (Larry Ullman’s tutorials):


<?php echo $form->dropDownList($model, 'idUserGroup', CHtml::listData(UserGroup::model()->findAll(), 'id', 'name')); ?>

The code works,and I get my dropDownList.

I tried on the attached class to just return the $model from the :loadGroups() method using the second piece of code but no luck either. var_dump(UserGroup::getGroups()) on _form.php returns NULL

You might wonder since it works why do you bother. I just need to understand. I like using things and code but I like to understand what it is that I use. This for me makes no sense. I know that this type of code will come in quiet handy. So please explain.

Thanks for your help.




	private static $_groups=array();


	....

	

	/**

	 * Access the $_group array

	 * @return array $_group array

	*/ 

	public static function getGroups(){	

	    if(!isset(self::$_groups))

                self::loadGroups();

            return self::$_groups;

	}



$_gourps is SET to an empty array.

So loadGroups() can never be called. :)

Bummer!

The blog tutorial uses $_groups[item] which does return false for isset, but I am using a single array which returns true for isset. I had no idea about that.

I could use this instead:


if(empty(self::$_groups))

                self::loadGroups();

or


if(!isset(self::$_groups[0]))

                self::loadGroups();

I thought the mistake was on loadGroups although I could see that it was never called because the trace never pulled through. It was a silly php question then :)

This is probably more of a coding preference question that I have. I know that for my purpose I want to use the class that I built but why would someone prefer the second method over the first.


<?php echo $form->dropDownList($model, 'idUserGroup', UserGroup::getGroups()); ?>

<?php echo $form->dropDownList($model, 'idUserGroup', CHtml::listData(UserGroup::model()->findAll(), 'id', 'name')); ?>

Thanks softark!