Best-Practice for drop-down of a foreign key in CRUD for a table?

I’m new to Yii (and I love it so far), but I have a question:

I created two tables, “user” and “role”. “user” table has a foreign key for a “role”. I used Gii to create CRUD’s for both tables. The “user” CRUD has a field to enter the foreign key for a “role”. I want that blank text field instead to be a populated drop-down of “role” names. I’m sure other’s have tackled this on their tables that have relationships. Is there a best-practice, extension or something built-in to Yii for doing this? Let me know if you need more information.

I figured I’d ask before I start working on something my own. Thank you!

Hey, welcome!

I’d suggest that you take a look at SRBAC extension. You will be able to handle roles and permissions easily.

hope this helps

regards!

:)

Using CHtml::listData() would be a normal way …




<div class="row">

<?php echo $form->labelEx($model,'role_id'); ?>

<?php

$role_list = CHtml::listData(Role::model()->findAll(), 'id', 'name');

$options = array(

	'tabindex' => '0',

	'empty' => '(not set)',

);

?>

<?php echo $form->dropDownList($model,'role_id', $role_list, $options); ?>

<?php echo $form->error($model,'role_id'); ?>

</div>



That’s interesting. I think that’s too robust for my needs and just for RBAC. I’m looking at how to deal with relationships in general, but I’ll have to look into SRBAC in the future, thank you.

Ok, this must be it then. Thank you for the detail; that really helps my noobness.

Thanks bro help me a lot :D