Hello Everyone,
It seem like no one has successfully implemented Batch Insert with yii.
What am trying to achieve is related to this. http://www.yiiframework.com/doc/guide/1.1/en/form.table
But in my own case, i want to insert multiple rows in which the fields are generated dynamically.
My models are attached. Please kindly help I have spent 24 Hours on this problem without a solution
[size="6"]This is my View[/size]
<?php
foreach($datas as $key=>$data) {
?>
<fieldset style="border:none;">
<legend></legend>
<h3 style="color:#777"><?php echo $data->title?></h3>
<?php echo $form->hiddenField($model,"[$key]title", array('class'=>'span8','value'=>$data->title)); ?>
<?php echo $form->html5EditorRow($model, "[$key]content", array('id'=>'ctn'.$data->id,'class'=>'span3', 'rows'=>16, 'height'=>'200', 'options'=>array('color'=>true))); ?>
<?php echo $form->hiddenField($model,"[$key]template_id",array('value'=>$data->id)); ?>
</fieldset>
<?php } ?>
[size="6"]My Controller Action[/size]
public function actionCreate()
{
$model=new Rfp;
$datas = Template::model()->findAll(array("order"=>'id ASC'));
$models=array();
if(isset($_POST['Rfp']) && isset($_POST['send'])) {
$valid=true;
foreach($_POST['Rfp'] as $j=>$model) {
if(isset($_POST['Rfp'][$j])) {
$model=new Rfp; // if you had static model only
$model->attributes=$_POST['Rfp'][$j];
$models[] = $_POST['Rfp'][$j];
$valid = $model->validate() && $valid;
}
}
if($valid) {
foreach($models as $v)
{
$v -> save();
}
$this->redirect(array('modelname/admin'));
}
}
$this->render('create',array(
'model'=>$model,'datas'=>$datas
));
}