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

Multi Model Transaction

$
0
0
Suggest a better way to implement below code

	public function actionCreate()
	{
		$model = new ProductRating;
		$ProductRatingDetail = new ProductRatingDetail;
		$Comment = new Comment;
				
				

		// UnComment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		$model->type = 'critic';
		if(isset($_POST['ProductRating']))
		{
			$model->attributes=$_POST['ProductRating'];
			if($model->save())
			{

				$ProductRatingDetail->product_rating_id = $model->id;

				$Comment->attributes = array(	 	 	 	 	
					'user_id'   => $model->user_id ,
					'comment'   => $_POST['Comment']['comment'],
					'>product_rating_id' => $model->id
				);

				if($ProductRatingDetail->save() and $Comment -> save())
				{
					$this->redirect(array('/ProductRatingDetails/view/'.$ProductRatingDetail->id));
				}
				else
				{
					if($ProductRatingDetail->id)
					{
						$ProductRatingDetail->delete();	
					}
					if($model->id)
					{
						$model->delete();	
					}
					if($Comment->id)
					{
						$Comment->delete();	
					}
				}
			}
		}

		$this->render('create',array(
			'model'=>$model, 'ProductRatingDetail'=> $ProductRatingDetail, 'Comment'=> $Comment
		));
	}



<div class="form">

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

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

	<?php //echo CHtml::errorSummary(array($model,  $Comment)); ?>

	<div class="control-group">
		<?php echo $form->labelEx($model,'user_id'); ?>
		<div class="control">
			<?php echo $form->dropDownList($model,'user_id', 
					$userSelected + CHtml::listData(User::model()->dropdown(array("critic","prouser")),'id','name','UserType.name') ,
					array('empty'=>'Select','required'=>'required')); ?>
			<?php echo $form->error($model,'user_id'); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'product_id'); ?>
		<div class="control">
			<?php echo $form->dropDownList($model,'product_id', 
					CHtml::listData(Product::model()->dropdown(),'id','name'),
					array('empty'=>'Select','required'=>'required')); ?>
			<?php echo $form->error($model,'product_id'); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'rate'); ?>
		<div class="control">
			<?php echo $form->dropDownList($model,'rate', range(0,10), 
					array('empty'=>'Select','required'=>'required')); ?>
			<?php echo $form->error($model,'rate'); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'type'); ?>
		<div class="control">
			<?php echo $form->dropDownList($model,'type', 
					ZHtml::enumItem( $model,'type'),
					array('required'=>'required')); ?>
			<?php echo $form->error($model,'type'); ?>
		</div>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($Comment,'comment'); ?>
		<div class="control">
			<?php echo $form->textArea($Comment,'comment',array('rows'=>6, 'cols'=>50)); ?>
			<?php echo $form->error($Comment,'comment'); ?>
		</div>
	</div>

	<div class="form-actions">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array ('class'=>'btn btn-primary')); ?>
	</div>

<?php $this->endWidget(); ?>

</div>&lt!-- form -->

Viewing all articles
Browse latest Browse all 18717