insert and update data to 2 tables

hi guys

I am newbie in yii and php, so please help me answer my problem

I have follow this link http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/, but in _form.php, there something confused.

it about the sentences …input fields for $a, $b… in that link. what is mean?

I have try like this




<?php echo $form->errorSummary(array($model,$model2)); ?>


	<div class="row">

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

		<?php echo $form->textField($model,$model2,'ktp',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error($model,$model2,'ktp'); ?>

	</div>


<div class="row buttons">

		<?php echo CHtml::submitButton ($model,$model2->isNewRecord ? 'Create' : 'Save'); ?>

	</div>




I got this error




strpos() expects parameter 1 to be string, object given



but, when I write this code




<?php echo $form->errorSummary(array($model,$model2));

     <div class="row">

		<?php echo $form->labelEx(array($model,$model2),'ktp'); ?>

		<?php echo $form->textField(array($model,$model2),'ktp',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error(array($model,$model2),'ktp'); ?>

	</div>


<div class="row buttons">

		<?php echo CHtml::submitButton (array($model,$model2->isNewRecord ? 'Create' : 'Save')); ?>

	</div>



I just got the blank page written Create Client

What wrong?

I use yii 1.1.7

and my controller syntax is




public function actionCreate()

        {

            $model=new Client;

            $model2=new DataMohon;

            if(isset ($_POST['Client'], $_POST['DataMohon']))

            {

                $model->attributeLabels=$_POST['Client'];

                $model2->attributeLabels=$_POST['DataMohon'];


                $valid=$model->validate();

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


                if($valid)

                {

                    $model->save(false);

                    $model2->save(false);

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

                }

            }


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

               'model'=>$model,

               'model2'=>$model2,

            ));

        }



I have try to search this question, but I have not found yet, my apologise if this problem have an answer, just show me that link.

sorry about my poor language

thanks guys for your help

I have review another post, it seem I have to use tabular input, but Sorry I don,t see the relation about this 2 artikel and answer my question, have any one can explain about this?

[color="#483D8B"]Hi friends ,

I used this steps for updating two tables using single controller ,

public function actionUpdate($id)

{


	&#036;model=&#036;this-&gt;loadModel(&#036;id);       // load model LeaveSummary


	


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


	{


		


	&#036;leaveDays = &#036;_POST['LeaveSummary']['LS_DAYS'];


	


	&#036;empModel = new Employee;           //  creating object for new model


	


	&#036;empData = &#036;empModel-&gt;getEmpData(&#036;_POST['LeaveSummary']['LS_EMPID']);     //  get employee data from id


            ...


            ...


            &#036;newUsedLeaves = &quot;xyz&quot;;


            &#036;empModel-&gt;updateLeave(&#036;id,&#036;newUsedLeaves);          //  function in model for updating data.


            ...


            ...


            // other code for updating data of leaveSummary model.


            ...


            ...


            }

}

this works 100%[/color]

First off, the tutorial you are reading teaches how to use one form and insert into two different models. While the code you showed seems like… two models in one textfield? I never tried that yet, but it seems like you cannot put two models in one textfield.

Though you could copy what is inputed in one model to another model. That is done on your controller file.

A good example would be:

controller file:




public function actionCreate()

        {

            $model=new Client;

            $model2=new DataMohon;

            if(isset ($_POST['Client'], $_POST['DataMohon']))

            {

                $model->attributeLabels=$_POST['Client'];

                $model2->attributeLabels=$_POST['DataMohon'];


                $valid=$model->validate();

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


                if($valid)

                {

                    $model->save(false);


                    $model2->ktp = $model->ktp;   //this saves what you inputed to your ktp textfield to both model and model2.


                    $model2->save(false);

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

                }

            }


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

               'model'=>$model,

               'model2'=>$model2,

            ));

        }



View:


<?php echo $form->errorSummary(array($model,$model2)); ?>


        <div class="row">

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

                <?php echo $form->textField($model,'ktp',array('size'=>20,'maxlength'=>20)); ?>

                <?php echo $form->error($model,'ktp'); ?>

        </div>


<div class="row buttons">

                <?php echo CHtml::submitButton ($model,$model2->isNewRecord ? 'Create' : 'Save'); ?>

        </div>



if this does not work, it probably is because there’s no model2 that was passed, and so the


           if(isset ($_POST['Client'], $_POST['DataMohon']))

won’t let you get pass through it unless you get at least one field with $model2 in it.

Even i am using a single form to insert values to two tables. But in my case i just want to insert a single field into the second table. Can someone help me in doing that?

i.e i want to store the user name in two tables and rest of the fields in the form to be inserted into a single table.


$model2->ktp = $model->ktp;   //this saves what you inputed to your ktp textfield to both model and model2.

this should do the trick. This should be placed in your controller file.

$model2->yourInputname is the second database that you want to copy the data into. For example:

You got this type of view:


<?php echo $form->errorSummary(array($model,$model2)); ?>


        <div class="row">

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

                <?php echo $form->textField($model,'ktp',array('size'=>20,'maxlength'=>20)); ?>

                <?php echo $form->error($model,'ktp'); ?>

        </div>


<div class="row buttons">

                <?php echo CHtml::submitButton ($model,$model2->isNewRecord ? 'Create' : 'Save'); ?>

        </div>

and you want to copy the text field “ktp” to both model1 and model2, you’ll have to do this to your controller:


public function actionCreate()

        {

            $model=new Client;

            $model2=new DataMohon;

            if(isset ($_POST['Client'], $_POST['DataMohon']))

            {

                $model->attributeLabels=$_POST['Client'];

                $model2->attributeLabels=$_POST['DataMohon'];


                $valid=$model->validate();

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


                if($valid)

                {

                    $model->save(false);


                    $model2->ktp = $model->ktp;   //this saves what you inputed to your ktp textfield to both model and model2.


                    $model2->save(false);

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

                }

            }


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

               'model'=>$model,

               'model2'=>$model2,

            ));

        }

hey when i use

==================

if(isset ($_POST[‘Client’], $_POST[‘DataMohon’]))

==================

Parse error: syntax error, unexpected ‘,’ in C:\xampp\htdocs\mywebapp\protected\controllers\AgentController.php on line 96

View:





<div class="row buttons">

                <?php echo CHtml::submitButton ($model,$model2->isNewRecord ? 'Create' : 'Save'); ?>

        </div>



unable to send two models using submitButton getting following error

Fatal error: Cannot unset string offsets in C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php on line 1901