activeDropDownList from another model

Dear all,

I just start using Yii Framework yesterday, I found it very easy to get start by following the blog tutorial, thank you for your hardwork!

I had few question hope I can get support from, I download the blog demo and look for it, I get some basic idea, but I would like to do this, but I dunno how, please your guys please guide it.

I got 2 tables, tbl_Role, tbl_User

tbl_Role

id role_name

1 Administrator

2 Normal User

3 Suspended User

tbl_User

id username password role_id

1 admin admin 1

2 nuser nuser 2

I got 2 models, one is User.php, another is Role.php

in the view for User, I would like to have a form to create user,

in the create user page, I got a drop down box for selecting role

the dropdownlist should get data from another model or database table.

how can I do this?

In the _form.php I try to do this

<?php echo CHtml::activeLabelEx($model,‘role’); ?>

<?php echo CHtml::activeDropDownList($model,‘role’,User::model()->getRoleOptions());?>

in the User model , I created this function,




	public function getRoleOptions()

	{

		$connection = Yii::app()->db;

		$command = "SELECT role_name from tbl_Role";

		$dataReader=$connection->createCommand($command)->query();

		$rows=$dataReader->readAll();

		return $rows;

		

	}



but what I get is got the ID and name, and I cannot store in the database,

I try use the relationship, but I don’t really get a lot understand in using relationship




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			 'role'=>array(self::BELONGS_TO, 'tbl_Role', 'id'),

		);

	}



This is just an example situation, I’m sure later I got a lot table need to join and select, hope I can get more understanding how to use it.

Thank you for your time.

This should display all of your roles in a dropdown list:


CHtml::activeDropDownList($model,'role_id',CHtml::listData( Role::model()->findAll(), 'id', 'role_name' ), array('prompt'=>'Select Role...'));

The first argument is the current model. This is used to create a default value on an edit view.

The second argument is an array of the data in the dropdown. The helper method ‘listData’ is used to more easily create this array for you.

BTW, I hope this works because I’m a noob too!

Thank you Makea for your reply, and it work for me.

Do you have idea how to get data from another table not from model, because some of my data need to get from the table, in my above situation I can use model, but what if I don’t have a model.

Thank you very much.


Role::model()->findAll()

The above code will fetch every record from the Role table. I used the same code in listdata to create your activedropdownlist.

The reference to the model was only used in the activedropdownlist so that it could automatically select a default value in an edit view.

You can add criterias to sort or find records based on conditions. Read the docs, they’re very good. The Definitive Guide to Yii is a good place to start to understand Yii’s DB handling.

Hi Makae, Thank you for your reply.

As I said in the first post, if I would like to get data from another table not in a model what should I do?

Currently I got this

I dunno how to get back the role name, I got read the Blog tutorial but that one is constant value, how should I do from database?

public function getRoleOptions()


{


	/*&#036;connection = Yii::app()-&gt;db;


	&#036;command = &quot;SELECT role from adm_role&quot;;


	&#036;dataReader=&#036;connection-&gt;createCommand(&#036;command)-&gt;query();


	&#036;rows=&#036;dataReader-&gt;read();


	return &#036;rows;*/





	&#036;_role=Adm_Role::model()-&gt;findAll();


	return &#036;_role;


}





public function getRoleText()


{


	&#036;options=&#036;this-&gt;roleOptions;


	return isset(&#036;options[&#036;this-&gt;role]) ? &#036;options[&#036;this-&gt;role] : &quot;unknown ({&#036;this-&gt;role})&quot;;


}

My current out put is unknown {1}

$rows=$db->createCommand($sql)->queryAll();

$roles=CHtml::listData($rows, ‘id’, ‘name’);

I find this post very useful. Thnx.