Yii Framework Forum: Inserting Numeric Value Of Item Which Is Displayed In Listbox To Another Table [Solved] - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   Mihkel. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-November 12

Posted 08 December 2012 - 05:31 AM

Hello,

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
					redirect(array('user/create'));
				}
				$this->roleid = $rolename->id;
...
}


Maybe there is any better solution for that or no?

Thanks.
0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 966
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 08 December 2012 - 05:40 AM

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


http://www.yiiframew...en/database.arr
0

#3 User is offline   Mihkel. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-November 12

Posted 08 December 2012 - 06:02 AM

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--')); ?>		
	</div>


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

#4 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 966
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 08 December 2012 - 06:58 AM

this could be the problem

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


to following

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

0

#5 User is offline   Mihkel. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-November 12

Posted 08 December 2012 - 07:25 AM

View Postalirz23, on 08 December 2012 - 06:58 AM, said:

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.
0

#6 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 966
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 08 December 2012 - 08:37 AM

glad I could help
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users