Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Submit Single Form Then Data Insert Into Two Tables

$
0
0
Hi,
I am new to yii framework. i want to do when i submit a form then the data will be inserted into multiple tables.
i tried many examples but not work for me.

i have 2 tables
1. Organization (org_id, org_name,org_code, org_desccription)
2. Contacts (contact_id, address1, address2, pincode)

views/Organization/_form.php

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'royce-organization-form',
'enableAjaxValidation'=>false,
));

//print_r($model);

//exit;
?>

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

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

<div class="row">
<?php echo $form->labelEx($model,'org_code'); ?>
<?php echo $form->textField($model,'org_code',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'org_code'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'org_name'); ?>
<?php echo $form->textField($model,'org_name',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'org_name'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'org_description'); ?>
<?php echo $form->textField($model,'org_description',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'org_description'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($contacts,'address1'); ?>
<?php echo $form->textArea($contacts,'address1',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($contacts,'address1'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($contacts,'address2'); ?>
<?php echo $form->textArea($contacts,'address2',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($contacts,'address2'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($contacts,'pincode'); ?>
<?php echo $form->textField($contacts,'pincode',array('size'=>17,'maxlength'=>17)); ?>
<?php echo $form->error($contacts,'pincode'); ?>
</div>
--------------------------------------------------------------------------------------------
views/Organization/create.php

<?php
/*$this->breadcrumbs=array(
'Organizations'=>array('index'),
'Create',
);

$this->menu=array(
array('label'=>'List Organization', 'url'=>array('index')),
array('label'=>'Manage Organization', 'url'=>array('admin')),
);*/
?>
<? require_once"side-bar.php";?>
<h1>Create Organization</h1>

<DIV CLASS="mynavigation" ID="mynavigation">
<?php

$this->beginWidget('zii.widgets.CPortlet', array(
'title'=>'Operations',
));

$this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'List Organization', 'url'=>array('index')),
array('label'=>'Manage Organization', 'url'=>array('admin')),
)
));
$this->endWidget(); ?>

</DIV>

<?php echo $this->renderPartial('_form', array('model'=>$model,'contacts'=>$contacts)); ?>

<? require_once"footer.php";?>

----------------------------------------------------------------------------------------------------------------
Organization Controller

public function actionCreate()
{
$model=new Organization;
$contacts = new Contacts();

if(isset($_POST['Organization']))
{

$model->attributes=$_POST['org_code'];
$model->attributes=$_POST['org_name'];
$model->attributes=$_POST['org_description'];

if($model->save())

if(isset($_POST['Contacts'])){
$contacts->attributes=$_POST['address1'];
$contacts->attributes=$_POST['address2'];
$contacts->attributes=$_POST['pincode'];

if($contacts->save())
$this->redirect(array('view','id'=>$contacts->contact_id));
}


}


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

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(

'contact' => array(self::BELONGS_TO, 'Contacts', 'contact_id'),
'address1' => array(self::BELONGS_TO, 'Contacts', 'address1'),
'address2' => array(self::BELONGS_TO, 'Contacts', 'address2'),
'pincode' => array(self::BELONGS_TO, 'Contacts', 'pincode'),

);
}
--------------------------------------------------------------------------------
someone posted solution for this problem inthis url and i follow this steps http://www.yiiframework.com/forum/index.php/topic/28441-yii-one-form-and-multiple-models/, but not work for me..Please help me!

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>