2016-09-13 5 views
0

나는 이것을 보았습니다 : 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>&nbsp;</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> 
+0

추가'''디버그 ($ 학생)''''''디버그 ($ this-> 요청 -> 데이터)'''patchEntity 전''' debug ($ student);'''patchEntity 다음에 질문을 올리십시오; 저장 옵션에서 '연관'을 제거하십시오. – Salines

+0

내일 할 수있는대로 바로 할 수 있습니다. 오늘 전화를 끊어야합니다. 당신의 도움을 주셔서 감사합니다! – pmelon

답변

0

변경 CakePHP 2에서 CakePHP 3 필드 이름 규칙을,

양식 제출에 PHP 폭풍 디버깅은 다음과 계시 Grade.{$key}.grade to grades.{$key}.grade

View/Elements/grades.ctp 파일을 다음 내용으로 작성하십시오. https://waltherlalk.com/blog/dynamic-form-input-fields

<?php 
$key = isset($key) ? $key : '<%= key %>'; 
?> 
<tr> 
    <td> 
     <?= $this->Form->hidden('grades.{$key}.id') ?> 
     <?= $this->Form->text('grades.{$key}.subject'); ?> 
    </td> 
    <td> 
     <?= $this->Form->select("grades.{$key}.grade", 
      [ 
      'A+', 
      'A', 
      'B+', 
      'B', 
      'C+', 
      'C', 
      'D', 
      'E', 
      'F' 
      ], 
      [ 
      'empty' => '-- Select grade --' 
      ]); ?> 
    </td> 
    <td class="actions"> 
     <a href="#" class="remove">Remove grade</a> 
    </td> 
</tr> 
+0

그 점은 고마워요. 미안 해요. 나는 그것이 컨트롤러와 관련이 있어야한다고 확신하지만, 어디에서 필요 한 작업을 찾을 수는 없다. – pmelon