Hi guys,
have two different tables and i’m using a single form to store into the two tables and there is no relation between that two tables.
Table 1: Forum_post
Table 2: user_gallery
And in my form im getting the status and image are input fields from two different tables.
[xml]forum_post table:
|id | user_id | ststus | photo_id |
|1 | 1 | hi…! | NULL |
|2 | 1 | hello! | NULL |
|3 | 1 | NULL | 1 |
|4 | 1 | NULL | 2 |
-----------------------------------[/xml]
[xml]user_gallery table:
|id | user_id | image | video |
|1 | 1 | 1.jpg | NULL |
|2 | 1 | new.gif| NULL |
[/xml]
_form.php
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'post-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo $form->textField($model,'user_id', array('value'=>Yii::app()->user->id), array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->textField($gallery,'user_id', array('value'=>Yii::app()->user->id), array('size'=>20,'maxlength'=>20)); ?>
</div>
<div class="row">
<?php echo $form->textArea($model,'content');
?>
</div>
<div>
<?php echo $form->fileField($gallery, 'forum_image', array()); ?>
</div>
<div>
<?php echo CHtml::submitButton('Upload',array()); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My controller action is:
public function actionUploadPost() {
$model = new ForumPost;
$gallery = new UserGallery;
if(isset($_POST['ForumPost'], $_POST['UserGallery'])) {
// populate input data to $model and $gallery
$model->attributes=$_POST['ForumPost'];
$gallery->attributes=$_POST['UserGallery'];
var_dump($gallery->attributes);
// validate BOTH $model and $gallaery
$valid=$model->validate();
$valid=$gallery->validate() && $valid;
if($valid)
{
// use false parameter to disable validation
$model->save(false);
$gallery->save(false);
// ...redirect to another page
}
}
$this -> render('_form, array('model' => $model, 'gallery' => $gallery));
}
On output when i’m printing the var_dump($gallery->attributes); only the user_id value gettin, but in var_dump($model->attributes); now i’m getting both user_id and status as in the array… Please help me… How to create a post using both image and content and store it in the different table…