Php 表格数据输入isn';他没有按预期工作。它';这只是在数据库中保存一条记录
我正在使用load multiple保存表格数据输入,但只有一条记录保存在数据库中,当我单击save(保存)按钮时,其余记录将自动填充与保存在数据库中的记录相同的数据。如何在yii中保存多个相同的表单字段 我在控制器中使用Model::loadMultiple保存模型的多个实例,并使用foreach循环通过计算用户保存的模型实例数来保存数组模型,在我看来,我在每个循环中都使用foreach循环来保存数组中的字段值 我希望在一个保存按钮上填写完所有字段后保存整个时间表,但它只在数据库中保存一条记录,当我只填写一个模型字段并单击保存时,所有字段都会自动填写 控制器代码:Php 表格数据输入isn';他没有按预期工作。它';这只是在数据库中保存一条记录,php,yii2,yii2-basic-app,Php,Yii2,Yii2 Basic App,我正在使用load multiple保存表格数据输入,但只有一条记录保存在数据库中,当我单击save(保存)按钮时,其余记录将自动填充与保存在数据库中的记录相同的数据。如何在yii中保存多个相同的表单字段 我在控制器中使用Model::loadMultiple保存模型的多个实例,并使用foreach循环通过计算用户保存的模型实例数来保存数组模型,在我看来,我在每个循环中都使用foreach循环来保存数组中的字段值 我希望在一个保存按钮上填写完所有字段后保存整个时间表,但它只在数据库中保存一条记录
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)]);
}
public function actionView($id)
{
//了解该表单提交了多少产品
$count=count(Yii::$app->request->post('timeline',[]);
//至少向表单发送一个模型
$modelstachedule=[新时间表()];
//创建已提交产品的数组
对于($i=1;$i<$count;$i++){
$modelstachedule[]=新时间表();
}
//加载并验证多个模型
if(Model::loadMultiple($modelstachedule,Yii::$app->request->post())和&Model::validateMultiple($modelstachedule)){
foreach($modeltime作为$modeltt){
//尝试保存模型。不需要验证,因为它是
已经完成了。
$modeltt->save(false);
}
}
返回$this->render('view',['modelstachedule'=>
$model时间表,'model'=>$this->findModel($id)];
}
查看代码:
<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(); ?>
说明:
// $modelTimetable[$key]->attributes = $value; //Can be comment
它也可以按如下方式使用:
if (Model::loadMultiple($modelTimetable , Yii::$app->request->post('form-name'), '') && Model::validateMultiple($modelTimetable)) {
说明:
// $modelTimetable[$key]->attributes = $value; //Can be comment
它也可以按如下方式使用:
if (Model::loadMultiple($modelTimetable , Yii::$app->request->post('form-name'), '') && Model::validateMultiple($modelTimetable)) {