Insert Multiple Values From A Field

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

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

Check this tutorial will help u

http://www.yiiframework.com/doc/guide/1.1/en/form.table

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">

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'selectedrolenames'); ?&gt;


	&lt;?php echo &#036;form-&gt;textArea(&#036;model,'selectedrolenames', array('rows' =&gt; 6, 'cols' =&gt; 50)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'selectedrolenames'); ?&gt;


&lt;/div&gt;

In model I added:

public $selectedrolenames;

In controller I am trying to add code in the Create action:

public function actionCreate()

{


	&#036;model=new Staffroleprofile;


        	


        	// AppDev - START


        	// create variable to the model Staffrolename...


        	&#036;model_staffrolename = new Staffrolename;


        	// AppDev - END





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['Staffroleprofile']))


	{


		&#036;model-&gt;attributes=&#036;_POST['Staffroleprofile'];


		if(&#036;model-&gt;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 '&#036;selectedrolenames', convert it into an array


                    	// and make entries in the table 'staffrolename'...


                    	//&#036;model_staffrolename-&gt;&#036;this-&gt;idstaffroleprofile=&#036;model-&gt;id;


                    	//&#036;model_staffrolename-&gt;username=&#036;model-&gt;selectedrolenames;


                    	//&#036;model_staffrolename-&gt;save();


                    	//INSERT INTO [appcommondata].[dbo].[staffrolename]


                    	//([idstaffroleprofile]


                   	// ,[username])


                   	// VALUES


                    	//(10


                   	// ,'some data')


                    	//&#036;model-&gt;attributes=&#036;_POST['Staffroleprofile'];


                    	//&#036;values = &quot;(&#036;model-&gt;id,  'somedata')&quot;;


                    	//&#036;values = &quot;(&#036;model-&gt;id,  &#036;this-&gt;selectedrolenames)&quot;;


                    	//&#036;_POST['selectedrolenames']


                    	//&#036;sql = 'INSERT INTO staffrolename (idstaffroleprofile, username) VALUES ' . &#036;values;


                    	//&#036;command = Yii::app()-&gt;db-&gt;createCommand(&#036;sql);


                    	//&#036;command-&gt;execute();


                    	&#036;model-&gt;StaffroleProfile;


                    	// AppDev - END


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


	}





	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;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

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