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

Problem With Batch Updating

$
0
0
i want to use batch updating and implement this code:

PostsController methods:
public function actionBatchUpdate()
	{
	   $models=null;
       $ids=null;
		if(isset($_POST['ids']))
        {
            $ids=$_POST['ids'];
            $models=$this->loadModels($ids);
        }

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
        
		if(isset($_POST['Posts']))
		{
            $isValid=true;
			foreach($models as $i=>$model)
            {
                if(isset($_POST['post'][$i]))
                {
                    $model->attributes=$_POST['Posts'][$i];
        			if($model->save())
                        $isValid=$isValid && true;
                }
    				
            }
            if($isValid)
                $this->redirect(array('index'));
		}
        
		$this->render('batchUpdate',array(
			'models'=>$models,
            'ids'=>$ids,
		));
	}
	public function loadModels($ids)
	{
	   $ids=preg_split('/\s*,\s*/',trim($ids));
		$models=Posts::model()->findAllByPk($ids);
		if($models===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $models;
	}


batchUpdate view:
<div class="form">

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

    <input type="text" name="ids" />
    
    <?php if(isset($models)) { ?>
    
	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php foreach($models as $i=>$model) { ?>
    
    <?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,"[$i]title"); ?>
		<?php echo $form->textField($model,"[$i]title",array('size'=>60,'maxlength'=>511)); ?>
		<?php echo $form->error($model,"[$i]title"); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,"[$i]content"); ?>
		<?php echo $form->textArea($model,"[$i]content",array('rows'=>6, 'cols'=>50)); ?>
		<?php echo $form->error($model,"[$i]content"); ?>
	</div>
    
    <?php } ?>

	<div class="row buttons">
		<?php echo CHtml::submitButton('Save'); ?>
	</div>
    
    <?php } ?>

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

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


everything is ok and fields are populating with correct attributes of model but when i want to update some of them there is no effect on data. any suggestion?
thanks in advance

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>