Inserting Numeric Value Of Item Which Is Displayed In Listbox To Another Table [Solved]


Another question from newbie. Actually I have working solution already in my hands but I’m just double checking this because Yii is soo automated ;) .

So, I have listbox which data comes from anothel Model. Table structure is ID and ITEM. So every item has unique ID. Now I’d like to insert ID to another table, it depends what user chooses right? My code:

public function BeforeSave() {

            if(parent::beforeSave()) {

                $rolename = Role::model()->findByAttributes(array('name'=>$this->roleid));

				if($rolename === null) {

					//for firebug hackers



				$this->roleid = $rolename->id;



Maybe there is any better solution for that or no?


If i am not mistaken you have to two tables which relate to each other if that is the the case simple relationship one-to-many in this case would be better option Yii does this out of the box what you have done please do take look at the guide

When I’m trying to submit the form without previous code I posted I get SQL syntax error which is quite obvious because it needs integer instead of string. Perhaps my view code is bad?

<div class="row">

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

		<?php echo $form->dropDownList($model ,'roleid',

                        CHtml::listData(Role::model()->findAll(), 'name', 'name'), 

                        array('empty' => '--Select Role--')); ?>		


Relation is there and this is working as I can access it trough another model.

this could be the problem

CHtml::listData(Role::model()->findAll(), 'name', 'name'),

to following

CHtml::listData(Role::model()->findAll(), 'id', 'name'),

Yes! Thank you :)

I knew that I made myself a more work to do ;) This was the reason for that topic.

glad I could help