I have two different table and i want to insert data in both of them at a time.
ONE table is verse
verse(id, topic_id, surah_id, verse_text) // id is primary key,
Second table is verse_translations
verse_translations(id, verse_id, language_id, translations_text) // id is primary key, language_id is foreign key references with language table, // verse_id is foreign key references with verse table.
Verse Create File (_form.php)
<div class="form">
<?php $form = $this->beginWidget('CActiveForm', array('id'=>'verse-form', 'enableAjaxValidation'=>true)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model, 'verse_text'); ?>
<?php echo $form->textArea($model, 'verse_text', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'verse_text'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'verse_translations'); ?>
<p class="<?php echo "tran".$model->id ?>">
<?php
$errors = array_filter($model->verseTranslations);
if(!empty($errors)) {
foreach($model->verseTranslations as $vt) {
echo $form->textArea($model, 'translation_text', array('value'=>$vt['translation_text'], 'rows'=>6, 'cols'=>50));
}
}
?>
</p>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
As you may see from _form file, I have called the data from verse_translations table.
Now my questions are:
How I keep the value of textArea that it will go in an array in controller? And How can I insert data in verse_translation table from verse create controller.
The output of _form file is like that.
Verse Create Controller Code.
public function actionCreate()
{
$model=new Verse;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Verse']))
{
$model->attributes=$_POST['Verse'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
Hope you understand it clearly.
Thanks
I think you should try this
public function actionCreate()
{
$verse_model=new Verse;
$verse_translation_model=new Verse_translations;
if(isset($_POST['Verse']) && isset($_POST['Verse_translations']))
{
$verse_model->attributes=$_POST['Verse'];
$verse_translation_model->attributes=$_POST['Verse_translations'];
$verse_model->save();
$verse_translation_model->save();
echo 'data is saved in both tables';
}
$this->render('create',array('verse_model'=>$verse_model,'verse_translation_model'=>$verse_translation_model));
}
In views in create.php
$this->renderPartial('_form',array('verse_model'=>$verse_model,'verse_translation_model'=>$verse_translation_model));
in _form.php
<?php $form = $this->beginWidget('CActiveForm', array('id'=>'verse-form', 'enableAjaxValidation'=>true)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($verse_model, 'verse_text'); ?>
<?php echo $form->textArea($verse_model, 'verse_text', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_model,'verse_text'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_model, 'topic_id'); ?>
<?php echo $form->textArea($verse_model, 'topic_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_model,'topic_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_model, 'surah_id'); ?>
<?php echo $form->textArea($verse_model, 'surah_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_model,'surah_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_transaction_model, 'verse_id'); ?>
<?php echo $form->textArea($verse_transaction_model, 'verse_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_transaction_model,'verse_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_transaction_model, 'translations_text'); ?>
<?php echo $form->textArea($verse_transaction_model, 'translations_text', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_transaction_model,'translations_text'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_transaction_model, 'language_id'); ?>
<?php echo $form->textArea($verse_transaction_model, 'language_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_transaction_model,'language_id'); ?>
</div>
To store in two tables (Model), you have to create two model object for each.
for ex:
$v_model = new Verse; // For save function
$vt_model = new VerseTransctions; // For save function
Pass these model object to view file and use as below.
For verse model text boxes, you have to use:
labelEx($v_model, 'verse_text');
For verse_tarnsction model text boxes, you have to use:
labelEx($vt_model, 'translation_text');
In action (save / update):
$v_model->attributes = $_POST["Verse"];
if($v_model->save())
{ $vt_model->attributes = $_POST["VerseTransctions"];
$vt_model->verse_id = $v_model->id; $vt_model->save();
}