Yii Framework Forum: Insert Multiple Values From A Field - Yii Framework Forum

Jump to content

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

Insert Multiple Values From A Field Rate Topic: -----

#1 User is offline   e25taki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 22-April 13

Posted 24 May 2013 - 07:51 PM

Hello.
Some time ago a decison was made at my workplace to "get rid of' our Domino environment and to look at other development environments. The decsion was made to go with PHP and MS SQL and we chose Yii as the framework. It is really exciting and also at time frustrating as we come to terms with this new Yii world.

I am comfortable now creating models, controllers and views using Gii and listing, creating and managing the tables and their records - Yii is just fantastic.

I am currently developing an application that has for example two table:
Table A and Table B

Table A:
id
profilename
business
site
type

Table B:
id
idA
personname

I have used yiic and Gii to create the application.
What I would like to know is when the View for model A is displayed I create a field listofnames that is a text box,. The other attributes profilename, business, site and type are already created by default for me. In this text box, listofnames, I can add the following; name1, name2, name3, name4, name5. When I save/create what I would like to do is get a handle to listofnames field and create entries in table A, also, when I load the form again I would like those list of names to populate my listofnames field and when I do a save again I need to ensure I remove those entries from table B and re-insert them (in the event listofnames has changed)

I know how to do this in normal PHP but what I was hoping for was some guidance on how to do it using just Yii.

Kind regards,

e25taki
0

#2 User is offline   Mohammad Tokallo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 52
  • Joined: 25-June 12

Posted 25 May 2013 - 03:01 AM

i didnt get exactly what you mean but if you mean you want add virtual attribute and according to that add multiple record once so
add $listofnames to TableA model as propert like so : public $listofnames

in view create CHtml::textField('listofnames') and access in controller as $_POST['listofnames']

So in controller you can explode as SPACE and Do What ever you want
0

#3 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 217
  • Joined: 04-October 12

Posted 25 May 2013 - 06:57 AM

Check this tutorial will help u

http://www.yiiframew...1/en/form.table
Yii :)
0

#4 User is offline   e25taki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 22-April 13

Posted 26 May 2013 - 07:37 PM

View PostMohammad Tokallo, on 25 May 2013 - 03:01 AM, said:

i didnt get exactly what you mean but if you mean you want add virtual attribute and according to that add multiple record once so
add $listofnames to TableA model as propert like so : public $listofnames

in view create CHtml::textField('listofnames') and access in controller as $_POST['listofnames']

So in controller you can explode as SPACE and Do What ever you want


Hello Mohammad,
thank you for yr reply however I feel i did not explain my problem well. What I I have is the following:
Table A: with columns id, rname, rtype
Table B: with columns id, idA, pname

What I am trying to achieve is an entry for

Table A eg, 125, application developer, newbie

and entries for
Table B eg
1, 125, John Smith
2, 125, Penny Smith
3, 125, Frank Smith

Now, the model created for Table A has attributes for id, rname and rtype. I created an additional attribute called $listofnames of type textbox. On the form this textbox is used to make name entries eg John Smith; Penny Smith; Frank Smith

In my 'A' model (for table A) how can I get a handle to the names in the textbox listofnames so I can then insert each name in Table B

I have tries $model->listofnames, $this->listof names but nothing sems to work in so far as getting a handle to the data.

In view I added:
<div class="row">
<?php echo $form->labelEx($model,'selectedrolenames'); ?>
<?php echo $form->textArea($model,'selectedrolenames', array('rows' => 6, 'cols' => 50)); ?>
<?php echo $form->error($model,'selectedrolenames'); ?>
</div>

In model I added:
public $selectedrolenames;

In controller I am trying to add code in the Create action:
public function actionCreate()
{
$model=new Staffroleprofile;

// AppDev - START
// create variable to the model Staffrolename...
$model_staffrolename = new Staffrolename;
// AppDev - END

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['Staffroleprofile']))
{
$model->attributes=$_POST['Staffroleprofile'];
if($model->save())
//AppDev - START
// we have saved our new staffroleprofile record and now have an id...
// what we want to do is now get the data entered in the attribute '$selectedrolenames', convert it into an array
// and make entries in the table 'staffrolename'...
//$model_staffrolename->$this->idstaffroleprofile=$model->id;
//$model_staffrolename->username=$model->selectedrolenames;
//$model_staffrolename->save();
//INSERT INTO [appcommondata].[dbo].[staffrolename]
//([idstaffroleprofile]
// ,[username])
// VALUES
//(10
// ,'some data')
//$model->attributes=$_POST['Staffroleprofile'];
//$values = "($model->id, 'somedata')";
//$values = "($model->id, $this->selectedrolenames)";
//$_POST['selectedrolenames']
//$sql = 'INSERT INTO staffrolename (idstaffroleprofile, username) VALUES ' . $values;
//$command = Yii::app()->db->createCommand($sql);
//$command->execute();
$model->StaffroleProfile;
// AppDev - END
$this->redirect(array('view','id'=>$model->id));
}

$this->render('create',array(
'model'=>$model,
));
}

Lots of commented out coed as I am trying stuff...

Hope this explanation helps in describing my issue (apart from a lack of Yii experience :(( )

Kind regards
e25taki
0

#5 User is offline   e25taki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 22-April 13

Posted 26 May 2013 - 09:33 PM

The following helped me as well as the above posts. I now can get access to the additional attribute.

http://www.yiiframew...rty-into-model/

Thanks heaps to all

Cheers!

Kind regards,
e25taki
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