Noob: Help With Adding Dropdownlist

Hi there,

I have two tables tbl_property and tbl_region.

When I create a new property I want to select a region from tbl_region.

So I’ve added a new function to /proptected/models/Property.php:


public function getRegionOptions()

	{

		$regionsArray = CHtml::listData($this->region, 'id', 'name');

		return $regionsArray;

	}

(Where ‘id’ and ‘name’ are the two columns in tbl_region).

And I’ve added a new relation to /proptected/models/Property.php:


'region' => array(self::BELONGS_TO, 'Region', 'region'),

(Where ‘region’ is a column in tbl_property)

Then I’ve altered /proptected/views/Property?_form.php to add the dropdown:


<?php echo $form->dropDownList($model,'region', $model->property->getRegionOptions()); ?>

But when I view the page I get a CExpection: Property "Property.property" is not defined.

Can anyone help me please?

you can create the array on function

for ex…




public function Showusernamedd()

    {

        $user_details = User::model()->findAll();

        $dd_data = array();

        if(isset($user_details) && !empty($user_details))

        {

            foreach($user_details AS $user)

            {

                $dd_data[$user->id] = $user->firstname.' '.$user->lastname.' ('.$user->username.') - '.$user->user_type;                  

            }

        }      

        return $dd_data;

    }

OK, so I removed ‘property’ from


<?php echo $form->dropDownList($model,'region', $model->property->getRegionOptions()); ?>

to give:


<?php echo $form->dropDownList($model,'region', $model->getRegionOptions()); ?>

Which got rid of the first error but now I have another one: Invalid argument supplied for foreach()

It seems to be pointing to the getRegionOptions() function:


public function getRegionOptions()

        {

                $regionsArray = CHtml::listData($this->region, 'id', 'name');

                return $regionsArray;

        }

Can anyone spot any problems with the parameters?

BTW I’m using the Web Application Development for Yii and PHP as a guide

Thanks Maggie Q, I used that function and it worked.

Can someone explain to me why the original function didn’t work?

also you can call like




property::model()->getRegionOptions()

and check this forum

full exmpale…

1)


<?php dropDownList($model, 'user_id', Users::model()->Showusernamedd()); ?>

2)




public function Showusernamedd()

    {

        $user_details = User::model()->findAll();

        $dd_data = array();

        if(isset($user_details) && !empty($user_details))

        {

            foreach($user_details AS $user)

            {

                $dd_data[$user->id] = $user->firstname.' '.$user->lastname.' ('.$user->username.') - '.$user->user_type;                  

            }

        }      

        return $dd_data;

    }

Maggie has given a clear explanation on how to call the Function from the View page,The error message "Invalid argument supplied for foreach" is caused due to the array.The array which returns the Result is null/empty so you have to check the not empty condition