Single form data to Multiple tables

Hi,

I have two tables (Event table and Event address table). But I would like to create a single form where user can fill Event details and address. And in the background I would like to store the information in two tables.

I created to Models. And then generated CRUD operations for both.

I struck with this.

Can anyone please help me with this?

Thank you in advance.

Regards,

Tarakarama

You can start with this wiki article - http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models

Hi MDOMBA,

Thank you for reply.

In the link i have seen the following code:

<?php echo CHtml::form(); ?>

//create.php – Here Do I need to paste create.php code?????

<?php echo $this->renderPartial(’_form’, array(‘a’=>$a,‘b’=>$B)); ?>

// _form.php – Here Do I need to paste _form.php code?????

<?php echo CHtml::errorSummary(array($a,$B)); ?>

…input fields for $a, $b…

</form>

That means that the following line (code) goes in that file…

Hi mdomba,

_form.php already covers input fields for $a

In that case why again input fields of $a.

…input fields for $a, $b…

You are sending two models to the view $a and $b… so you make here input fields for both models as you need them… if you already have for $a just add those for model $b…

If you still don’t understand… it’s better if you post here your code… and write what you need to accomplish…

Hi mdomba,

To be honest with you. I am not good at framework. I have two tables (Event and EventAddress).

As a first step, I created two models and generated CRUD for both.

Then I went to views and opened event folder and under that _form.php is there.

I am pasting _form.php code in EventAddress views folder.

Here I am sending the code.

Sorry to bother you. I am learning a lot here. Thank you for your support.

Code:

<div class="form">

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

'id'=&gt;'event-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model,&#036;address); ?&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'TITLE',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'PURPOSE',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'AMOUNT',array('size'=&gt;10,'maxlength'=&gt;10)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


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


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'USER_ID',array('size'=&gt;10,'maxlength'=&gt;10)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'STATUS',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;address,'ADDRESS_ID',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;address,'ORG_DATE',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;address,'COUNTRY_ID',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;address,'STATE_ID',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;address,'CITY_ID',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;address,'LINE1',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;address,'LINE2',array('size'=&gt;45,'maxlength'=&gt;45)); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;

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

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

First… when you post code… use the code button ( <> - on the toolbar of the editor)

Second… we all do know what code is generated by Gii… there is no need to paste all the code…

Third… as I wrote above… you need to explain what you want to do

Forth (should be first)… Have you read and understood the Definitive guide to Yii…

IMHO… you need first to understand the concepts of how all this works… how the controller gets the data from the model and send them to the view… for simple situations… then we can move to more complex one…

Hi mdomba,

I agree with you. However I mentioned before that I am a newbie. But I have php knowledge and oops knowledge.

One thing I would like to clarify from your answer. Does it mean, there is no possibility of doing so called complex things in YII framework ?

The guide you have mentioned is not giving straight picture on how to do things. Anyway as you suggested I will read one more time. But How do I know that I reached a position to ask complex questions that are not covered in your guide? Is there any measure?

Please let me know.

Thank you,

taraka

You did not understand exactly what I wrote… it is possible to do all complex things in Yii… but from your posts I saw that you do not understand even the simple one so you cannot understand the complex one…

the wiki article explains all very well… and I even explained a bit more on passing two models to the view… but you do not understand it… so I don’t know how else to help you…

even if you would send me your code with the database structure… and I wrote you the code you need… you would not understand it… and this way you would not learn anything… (IMHO)…

So you have to get first the basics… to understand… how a controller/model/view works… for this special case… you need to understand how the controller pass a variable to the view… when you will understand this… than you problem will be solved…

Hi takarama.

You can be in 2 situation:

1: you have exactly 1 model of Event of exactly 1 model of EventAddress

This situation is easy to manage, take a look at this wiki, is all clearly explained.

2: You have 2 model of Event and 0-1-more models of EventAddress.

This is a difficoult task, but in this using this extension you can achive.

As is a difficoult task, is better for you to leave suspended a bit and move forward in implementation of your application, since you will be more skilled in the framework.

thanks for the wiki link ! :)

I want to share the codes if you want to save data in two tables.

the scenario is: I have 2 tables, user and member. I want to make one form that includes member_name, password, email, and ref_id. But, I want to save that informations into both tables. member_name, password and ref_id for user table and also member_name, email and ref_id for member table.

so how the tricks done? first I add passwordField in the member’s _form.php


<div class="row">

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

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

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

    </div>

secondly, I edit the Member model by adding this AfterSave function :


 public function afterSave() {

        if($this->isNewRecord){

            $user = new User();

            $user->username = $this->member_name;

            $user->password = $this->password;

            $user->ref_id = $this->ref_id;

            $user->save();

        }

    }

don’t forget to add the rules and attributeLabels in the Member model too :))

that’s it. I hope this will help someone

Hi, dyooolicious! Thanks for the code. It works well to me and now I can save the data into two tables. :lol: