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.