나는 이것을 보았습니다 : https://waltherlalk.com/blog/dynamic-form-input-fields이에서 활성화되었습니다 : Dynamically add form field rows - cakePHP. 설정이 monsur.hoq의 Stackoverflow 게시물에 따라 변경된 원래 튜토리얼에 따라 단계에 도달했습니다.Cakephp 3의 동적 양식 입력 필드
양식이 정상적으로 작동하지만 저장시 데이터의 '학생'부분 만 저장됩니다. 성적은 보내지 않습니다. 내 컨트롤러의 추가 부분은 현재 다음과 같습니다.
public function add()
{
$student = $this->Students->newEntity();
if ($this->request->is('post')) {
$student = $this->Students->patchEntity($student, $this->request->data);
if ($this->Students->save($student)) {
$this->Flash->success(__('The student has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The student could not be saved. Please, try again.'));
}
}
$this->set(compact('student'));
$this->set('_serialize', ['student']);
}
모든 코드는 monsur.hoq 게시물에 의해 모양이 지정됩니다.
누군가 Walther Lalk 튜토리얼의 Cakephp3 예제를 작성하는 데 도움을 줄 수 있다면 매우 감사 할 것입니다.
디버깅 도구 모음은 다음과 같은 SQL이 양식을 제출에서 생산되는 보여줍니다
INSERT INTO students (name, created, modified)
VALUES
(
'Test Two', '2016-09-13 16:04:07',
'2016-09-13 16:04:07'
)
모든이가해야 할 역할이 문제를 확인합니다.
이$_POST = {array} [3]
_method = "POST"
name = "Test Four"
Grade = {array} [1]
0 = {array} [3]
id = ""
subject = "Maths"
grade = "3"
add.ctp은 다음과 같다 :
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Students'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Grades'), ['controller' => 'Grades', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Grade'), ['controller' => 'Grades', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="students form large-9 medium-8 columns content">
<?= $this->Form->create($student) ?>
<fieldset>
<legend><?= __('Add Student') ?></legend>
<?php
echo $this->Form->input('name');
?>
</fieldset>
<fieldset>
<legend><?php echo __('Grades');?></legend>
<table id="grade-table">
<thead>
<tr>
<th>Subject</th>
<th>Grade achieved</th>
<th> </th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td class="actions">
<a href="#" class="add">Add grade</a>
</td>
</tr>
</tfoot>
</table>
</fieldset>
<script id="grade-template" type="text/x-underscore-template">
<?php echo $this->element('grades');?>
</script>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
<script>
$(document).ready(function() {
//I changed undescore default template settings
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
}
var
gradeTable = $('#grade-table'),
gradeBody = gradeTable.find('tbody'),
gradeTemplate = _.template($('#grade-template').remove().text()),
numberRows = gradeTable.find('tbody > tr').length;
gradeTable
.on('click', 'a.add', function(e) {
e.preventDefault();
$(gradeTemplate({key: numberRows++}))
.hide()
.appendTo(gradeBody)
.fadeIn('fast');
})
.on('click', 'a.remove', function(e) {
e.preventDefault();
$(this)
.closest('tr')
.fadeOut('fast', function() {
$(this).remove();
});
});
if (numberRows === 0) {
gradeTable.find('a.add').click();
}
});
</script>
추가'''디버그 ($ 학생)''''''디버그 ($ this-> 요청 -> 데이터)'''patchEntity 전''' debug ($ student);'''patchEntity 다음에 질문을 올리십시오; 저장 옵션에서 '연관'을 제거하십시오. – Salines
내일 할 수있는대로 바로 할 수 있습니다. 오늘 전화를 끊어야합니다. 당신의 도움을 주셔서 감사합니다! – pmelon