Yii Framework Forum: Submit Single Form Then Data Insert Into Two Tables - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Submit Single Form Then Data Insert Into Two Tables Rate Topic: -----

#1 User is offline   Nandhagopal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-January 13

Posted 13 February 2013 - 08:12 AM

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.yiiframew...index.php/topic/28441-yii-one-form-and-multiple-models/, but not work for me..Please help me!
0

#2 User is offline   kutti 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-February 13

Posted 16 February 2013 - 06:51 AM

Hi Nanda,

Try this code in your controller section

Organization Controller

public function actionCreate()
{

$model=new Organization;
$contacts = new Contacts();

if(isset($_POST))
{

if($_POST['Organization']) {
$model->attributes=$_POST['Organization'];
$model->save();
}

if(isset($_POST['Contacts'])){
$contacts->attributes=$_POST['Contacts'];
$contacts->save();
}
//Redirect to your target page
$this->redirect('redirection path');

}

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

#3 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 16 February 2013 - 12:59 PM

/* moved from Bug Discussions */
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users