On Submit Store Values Into Two Different Tables

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…

if you use attributes then properties in the table model and the name of fields in the form must have identical name

in you tables only user_id is identical in the tables and form

use simple Post values for other form fields

$gallery->image=Yii::app()->request->getPost(<fieldname of image>);

Thanks for response Rosomaha. I have successfully stored the images and content in the different table.

Controller:


public function actionUploadPost() {

 	$model = new ForumPost;

 	$gallery = new UserGallery;

     	if(isset($_POST['ForumPost'], $_POST['UserGallery'])) {

         $model->attributes=$_POST['ForumPost'];

         $gallery->attributes=$_POST['UserGallery'];

 	$rnd = rand(0123456789, 9876543210);

 	$timeStamp = time();

 	$uploadedFile = CUploadedFile::getInstance($gallery, 'forum_image');

 		if ($uploadedFile != null) {

 			$fileName = "{$rnd}-{$timeStamp}-{$uploadedFile}";

  			$gallery -> forum_image = $fileName;

 		}

 		$valid_format = "jpg,png,jpeg,gif";

  		if ($gallery -> save() && $valid_format) {

 			$model -> photo_id = $gallery -> id;

 			$model -> save(FALSE);

 			if (!empty($uploadedFile)) {

 				$uploadedFile -> saveAs(Yii::app() -> basePath . '/../images/post/' . $fileName);

 			}

 		}

     	}

 	$this -> render('_uploadStatusImage', array('model' => $model, 'gallery' => $gallery));

}

Now what i want to do, when user only post a content i don’t want to create one row in the image table. Now there is one Null value row is creating and refers to the post table. Please help me to move forward. :(

you $gallery->save() must be in condition IF $uploadedFile != null, and work with model must be separately

	if (&#036;uploadedFile &#33;= null) {


                 ///////////////////////////


                 uploadedFile -&gt; saveAs(Yii::app() -&gt; basePath . '/&#46;&#46;/images/post/' . &#036;fileName);


                 if (is_file(Yii::app() -&gt; basePath . '/&#46;&#46;/images/post/' . &#036;fileName)){


                        &#036;gallery -&gt; save()


                 }


	}


        /////////////////////////////////////


            work with model