add value to array

Hi everyone…

I’m getting the id of latest inserted user and append it to the

$_POST[‘Basicdetails’] then saved it.

please help




public function actionCreate()

	{

		$model=new Basicdetails;

		$modeluseraccounts= new Useraccounts;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Basicdetails']))

		{

			$model->attributes=$_POST['Basicdetails'];

			

			$modeluseraccounts->attributes=Yii::app()->session['Useraccounts'];

			$model->attributes=$modeluseraccounts->id . $_POST['Basicdetails'];

			$modeluseraccounts->save();

			

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

You can’t use the “.” syntax when adding a value to an array. This doesn’t make sense. Try this:




//CHANGE THIS $model->attributes=$modeluseraccounts->id . $_POST['Basicdetails']; TO

$model->keyAttribute=$modeluseraccounts->id;



where "keyAttribute" is the name of the foreign key field in the BasicDetails table.

Hope it helps,

Hannes

Hi haensel, still it does not work

here is the error







CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`db_prod`.`basicdetails`, CONSTRAINT `basicdetails_ibfk_41` FOREIGN KEY (`userid`) REFERENCES `useraccounts` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `basicdetails` (`haircolor`, `eyecolor`, `height`, `weight`, `bodytype`, `ethnicity`, `appearance`, `do_you_drink`, `do_you_smoke`, `marital_status`, `have_children`, `more_children`, `occupation`, `willing_to_relocate`, `relationship_your_looking`, `nationality`, `education`, `english_language_ability`, `religion`, `star_sign`, `profile_heading`, `about_yourself`, `what_youre_looking_for`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7, :yp8, :yp9, :yp10, :yp11, :yp12, :yp13, :yp14, :yp15, :yp16, :yp17, :yp18, :yp19, :yp20, :yp21, :yp22)



If the id field is autoincremented by the database… you cannot use it before saving the data… reverse the 2 lines so to get:




$modeluseraccounts->save();

$model->attributes=$modeluseraccounts->id;



Yessssss! it is! :) thanks masters…

Note that your code is not optimal… as what will happen if you get a validation error in $model->save()… the $modeluseraccount is already saved at that time…

Check this article to see how to first validate both models and then save them - http://www.yiiframework.com/wiki/218

sir mdomba I tried the article but I got errors,

[color="#FF0000"]just noticed here the variable $b keeps changing in $B even I type $b at render part.[/color]

controller





public function actionCreate()

	{

		$a=new Useraccounts;

		$b=new Basicdetails;

		


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

		

		$this->performAjaxValidation(array($a,$<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />);

		if(isset($_POST['Useraccounts'],$_POST['Basicdetails']))

		{

			 // populate input data to $a and $b

			$a->attributes=$_POST['Useraccounts'];

			$b->attributes=$_POST['Basicdetails'];

				

			// validate BOTH $a and $b

			$valid=$a->validate();

			$valid=$b->validate() && $valid;

	 

			if($valid)

			{

				// use false parameter to disable validation

				$a->save(false);

				$b->save(false);

	 

				$this->redirect('index');

			}

		}

		$this->render('create',array('a'=>$a,'b'=>$<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />);

	}




view:





<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'useraccounts-form',

	'enableAjaxValidation'=>true,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary(array($a,$<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />); ?>


	<div class="row">

		<?php echo $form->labelEx($a,'fname'); ?>

		<?php echo $form->textField($a,'fname',array('size'=>30,'maxlength'=>30)); ?>

		<?php echo $form->error($a,'fname'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($a,'lname'); ?>

		<?php echo $form->textField($a,'lname',array('size'=>30,'maxlength'=>30)); ?>

		<?php echo $form->error($a,'lname'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($a,'password'); ?>

		<?php echo $form->passwordField($a,'password',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error($a,'password'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($a,'password_repeat'); ?>

		<?php echo $form->passwordField($a,'password_repeat',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error($a,'password_repeat'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($a,'email'); ?>

		<?php echo $form->textField($a,'email',array('size'=>50,'maxlength'=>50)); ?>

		<?php echo $form->error($a,'email'); ?>

	</div>


	<div class="row inline">

		<?php echo $form->labelEx($a,'gender'); ?>

		<?php

				echo $form->radioButtonList($a, 'gender',

					array(  0 => 'Male',

							1 => 'Female'),

					array( 'separator' => "&nbsp;&nbsp;" ) ); // choose your own separator text

		?>

		<?php echo $form->error($a,'gender'); ?>

	</div>


	<div class="row inline">

		<?php echo $form->labelEx($a,'seeking'); ?>

		<?php

				echo $form->radioButtonList($a, 'seeking',

					array(  0 => 'Male',

							1 => 'Female'),

					array( 'separator' => "&nbsp;&nbsp;" ) ); // choose your own separator text

		?>

		<?php echo $form->error($a,'seeking'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($a,'dob'); ?>

		<?php Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker');

				$this->widget('CJuiDateTimePicker',array(

					'model'=>$a, //Model object

					'attribute'=>'dob', //attribute name

					'mode'=>'date', //use "time","date" or "datetime" (default)

					'options'=>array(

						'dateFormat'=>'yy-mm-dd',

						'changeMonth'=> true,

						'changeYear'=> true,

					), // jquery plugin options

					'language' => '',

				));

		?>

		<?php echo $form->error($a,'dob'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($a,'country'); ?>

		<?php echo $form->dropdownlist($a,'country',$a->getCountryoptions()); ?>

		<?php echo $form->error($a,'country'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($a,'state_province'); ?>

		<?php echo $form->dropdownlist($a,'state_province',$a->getProvinceoptions()); ?>

		<?php echo $form->error($a,'state_province'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($a,'city'); ?>

		<?php echo $form->dropdownlist($a,'city',$a->getTownoptions()); ?>

		<?php echo $form->error($a,'city'); ?>

	</div>

	

	

	<div class="row">

		<?php echo $form->labelEx($b,'haircolor'); ?>

		<?php echo $form->dropDownlist($b,'haircolor',CHtml::listData(haircolormaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'haircolor'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'eyecolor'); ?>

		<?php echo $form->dropDownlist($b,'eyecolor',CHtml::listData(eyecolormaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'eyecolor'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'height'); ?>

		<?php echo $form->dropDownlist($b,'height',CHtml::listData(heightmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'height'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'weight'); ?>

		<?php echo $form->dropDownlist($b,'weight',Chtml::listData(Weightmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'weight'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'bodytype'); ?>

		<?php echo $form->dropDownlist($b,'bodytype',CHtml::listData(Bodytypemaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'bodytype'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'ethnicity'); ?>

		<?php echo $form->dropDownlist($b,'ethnicity',CHtml::listData(Ethnicitymaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'ethnicity'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'appearance'); ?>

		<?php echo $form->dropDownlist($b,'appearance',CHtml::listData(appearancemaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'appearance'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'do_you_drink'); ?>

		<?php echo $form->dropDownlist($b,'do_you_drink',CHtml::listData(drinkmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'do_you_drink'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'do_you_smoke'); ?>

		<?php echo $form->dropDownlist($b,'do_you_smoke',CHtml::listData(smokemaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'do_you_smoke'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'marital_status'); ?>

		<?php echo $form->dropDownlist($b,'marital_status',CHtml::listData(maritalmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'marital_status'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'have_children'); ?>

		<?php echo $form->dropDownlist($b,'have_children',CHtml::listData(havechildrenmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'have_children'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'more_children'); ?>

		<?php echo $form->dropDownlist($b,'more_children',CHtml::listData(wantmorechildrenmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'more_children'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'occupation'); ?>

		<?php echo $form->dropDownlist($b,'occupation',CHtml::listData(occupationmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'occupation'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'willing_to_relocate'); ?>

		<?php echo $form->dropDownlist($b,'willing_to_relocate',CHtml::listData(relocatemaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'willing_to_relocate'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'relationship_your_looking'); ?>

		<?php echo $form->dropDownlist($b,'relationship_your_looking',CHtml::listData(relationshipmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'relationship_your_looking'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'nationality'); ?>

		<?php echo $form->dropDownlist($b,'nationality',CHtml::listData(nationalitymaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'nationality'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'education'); ?>

		<?php echo $form->dropDownlist($b,'education',CHtml::listData(educationmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'education'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'english_language_ability'); ?>

		<?php echo $form->dropDownlist($b,'english_language_ability',CHtml::listData(englishabilitymaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'english_language_ability'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'religion'); ?>

		<?php echo $form->dropDownlist($b,'religion',CHtml::listData(religionmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'religion'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'star_sign'); ?>

		<?php echo $form->dropDownlist($b,'star_sign',CHtml::listData(starsignmaster::model()->findAll(),'id','description'),array('empty'=>'--- Please Select ---')); ?>

		<?php echo $form->error($b,'star_sign'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'profile_heading'); ?>

		<?php echo $form->textField($b,'profile_heading',array('size'=>60,'maxlength'=>500)); ?>

		<?php echo $form->error($b,'profile_heading'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'about_yourself'); ?>

		<?php echo $form->textArea($b,'about_yourself',array('size'=>60,'maxlength'=>500)); ?>

		<?php echo $form->error($b,'about_yourself'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($b,'what_youre_looking_for'); ?>

		<?php echo $form->textArea($b,'what_youre_looking_for',array('size'=>60,'maxlength'=>500)); ?>

		<?php echo $form->error($b,'what_youre_looking_for'); ?>

	</div>

	

	

	<div class="row buttons">

		<?php echo CHtml::submitButton($a->isNewRecord ? 'Continue' : 'Save'); ?>

	</div>

	

<?php $this->endWidget(); ?>


</div><!-- form -->



what is the error?

note that in the new code you are not assigning the userAccounts ID

the error appears just when enter in the url the action for create

I am using version 1.8 sir

here is the error sir

Create Useraccounts

Fields with * are required.

( ! ) Fatal error: Call to a member function getErrors() on a non-object in D:\xampp\htdocs\yii\framework\web\helpers\CHtml.php on line 1605

Call Stack

Time Memory Function Location

1 0.0015 318448 {main}( ) …\index.php:0

2 0.0568 1879568 CApplication->run( ) …\index.php:13

3 0.0569 1879568 CWebApplication->processRequest( ) …\CApplication.php:158

4 0.0643 2102312 CWebApplication->runController( ) …\CWebApplication.php:136

5 0.0751 2530904 CController->run( ) …\CWebApplication.php:277

6 0.0771 2572704 CController->runActionWithFilters( ) …\CController.php:257

7 0.0803 2651000 CFilterChain->run( ) …\CController.php:283

8 0.0807 2651976 CInlineFilter->filter( ) …\CFilterChain.php:131

9 0.0808 2652040 CController->filterAccessControl( ) …\CInlineFilter.php:59

10 0.0837 2736728 CFilter->filter( ) …\CController.php:1144

11 0.0981 3050624 CFilterChain->run( ) …\CFilter.php:41

12 0.0981 3050624 CController->runAction( ) …\CFilterChain.php:134

13 0.0981 3050624 CInlineAction->runWithParams( ) …\CController.php:300

14 0.0982 3051240 UseraccountsController->actionCreate( ) …\CInlineAction.php:50

15 0.1726 4965832 CController->render( ) …\UseraccountsController.php:92

16 0.1726 4965896 CController->renderPartial( ) …\CController.php:779

17 0.1731 4966152 CBaseController->renderFile( ) …\CController.php:866

18 0.1731 4966216 CBaseController->renderInternal( ) …\CBaseController.php:88

19 0.1734 5011808 require( ‘D:\xampp\htdocs\filipinocupid\protected\views\useraccounts\create.php’ ) …\CBaseController.php:119

20 0.1734 5013744 CController->renderPartial( ) …\create.php:15

21 0.1737 5013952 CBaseController->renderFile( ) …\CController.php:866

22 0.1737 5014016 CBaseController->renderInternal( ) …\CBaseController.php:88

23 0.1753 5221520 require( ‘D:\xampp\htdocs\filipinocupid\protected\views\useraccounts\_form.php’ ) …\CBaseController.php:119

24 0.1856 5995064 CActiveForm->errorSummary( ) …\_form.php:10

25 0.1856 5995360 CHtml::errorSummary( ) …\CActiveForm.php:511

you pass the variable $a and $b to the "create.php" view… check that view… there you need to pass again those variables to the "_view.php" view…

We’ve done it… yes!!! :) now I can sleep, It’s almost 10:00… thanks for the help sir, I really appreciate it.