I'm using load multiple for saving tabular data input but only one record is being saved in database and the remaining ones are then automatically filled with the same data as the one saved in database when i click save button.How do i save multiple same form fields in yii?
I've used Model::loadMultiple in my controller to save multiple instances of a model and used foreach loop to save array model by counting the number of model instances saved by the user and in my view i've used for each loop to save field values in array.
I want whole timetable to be saved when all the fields are filled, on one save button,but it just saves only one record in database and all the fields are filled automatically when i fill just one model fields and click save.
Controller code :
public function actionView($id)
{
//Find out how many products have been submitted by the form
$count = count(Yii::$app->request->post('Timetable', []));
//Send at least one model to the form
$modelTimetable = [new Timetable()];
//Create an array of the products submitted
for($i = 1; $i < $count; $i++) {
$modelTimetable[] = new Timetable();
}
//Load and validate the multiple models
if (Model::loadMultiple($modelTimetable, Yii::$app->request->post()) && Model::validateMultiple($modelTimetable)) {
foreach ($modelTimetable as $modeltt) {
//Try to save the models. Validation is not needed as it's
already been done.
$modeltt->save(false);
}
}
return $this->render('view', ['modelTimetable' =>
$modelTimetable,'model' => $this->findModel($id)]);
}
View Code:
<div class="Timetableform">
<? @var $modelTimetable app\models\Timetable; ?>
</div><!-- Timetableform -->
<?php $form = ActiveForm::begin(); ?>
<?php
$query1=new \yii\db\Query();
$query2=new \yii\db\Query();
$row1 = $query1->select(["CONCAT(classes.Start_Time, '-',
classes.End_Time) AS classunit"])->from('class_units AS classes')-
>all();
$row2=$query2->select(['Day_name'])->from('days')->all();
$counter = 0;
echo '<div class="col-xs-9">
<div class="table-responsive">
<table class="table table-striped" id="tt_data">
<tr>
<td class="blank"></td>';
foreach ($row1 as $rowunits) {
if(!empty($rowunits)){
echo '<td class="title">
'.$rowunits['classunit'].'<br></td>';
}
}
echo '</tr>';
foreach ($row2 as $rowdays) {
echo '<tr>';
echo '<td class="time">'.$rowdays['Day_name'].'</td>';
for ($i= 0; $i < 7; $i++ )
{
foreach ($modelTimetable as $index => $modeltt) {
echo '<td class="drop">'.$form->field($modeltt, "[$counter]TAC_id")-
>dropDownList( ArrayHelper::map(teacherassignedcourses::find()-
>joinWith(['teachers'])->joinWith(['courses'])-
>all(),'TAC_id','courses.Course_title', 'teachers.Teacher_Name'),
['prompt'=>'Teacher'])->label(false) ?>
<?= $form->field($modeltt, "[$counter]Room_id")-
>dropDownList(ArrayHelper::map(rooms::find()-
>all(),'Room_id','Room_Number'),['prompt'=>'Room'])->label(false) ?>
<?= $form->field($modeltt, "[$counter]Class_type") ->textInput()-
>label(false) ?>
<?= $form->field($modeltt, "[$counter]Shift") ->textInput()-
>label(false) ?>
<?= $form->field($modeltt, "[$counter]Batch_id") ->textInput()-
>label(false) ?>
<?= $form->field($modeltt, "[$counter]Day_id")->textInput()-
>label(false) ?>
<?= $form->field($modeltt, "[$counter]Unit_id")->textInput()-
>label(false).
'</td>';
}
$counter++;
}
echo '</tr>';
}
echo '</table>
</div>
</div>';
?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<script type="text/javascript">
var nodes = document.querySelectorAll("td.title");
for(var i = 0; i < nodes.length; i++) {
nodes[i].setAttribute("id", i+'a');
}
var nodes1 = document.querySelectorAll("td.time");
for(var i = 0; i < nodes1.length; i++) {
nodes1[i].setAttribute("id", i+'b');
}
</script>
<?php ActiveForm::end(); ?>