2017-11-30 1 views
0

저는 cakephp의 초보자이고 이제는 cakephp 3.5를 사용하고 있습니다. 양식을 만들어야 할 때 문제가 있습니다. 내가 만들고자하는 양식은 인덱스보기에서 가져온 것이고 여러 개의 확인란이 있습니다. 내 문제는 확인란을 클릭 할 때마다 데이터가 저장되지 않는다는 것입니다.인덱스를 병합하여 cakephp의 폼에 액션 추가 3.5

양식의 이름은 existing_question.ctp입니다. 여기에 코드가 있습니다.

<?php 
/** 
* @var \App\View\AppView $this 
* @var \App\Model\Entity\Question[]|\Cake\Collection\CollectionInterface $questions 
*/ 
?> 
<nav class="large-3 medium-4 columns" id="actions-sidebar"> 
    <ul class="side-nav"> 
     <li class="heading"><?= __('Actions') ?></li> 
     <li><?= $this->Html->link(__('New Question'), ['action' => 'add']) ?></li> 
     <li><?= $this->Html->link(__('List Question Quizzes'), ['controller' => 'QuestionQuizzes', 'action' => 'index']) ?></li> 
     <li><?= $this->Html->link(__('New Question Quiz'), ['controller' => 'QuestionQuizzes', 'action' => 'add']) ?></li> 
    </ul> 
</nav> 
<div class="questions index large-9 medium-8 columns content"> 
    <h3><?= __('Questions') ?></h3> 
    <?= $this->Form->create($questions) ?> 
    <table cellpadding="0" cellspacing="0"> 
     <thead> 
      <tr> 
       <th scope="col"><?= $this->Paginator->sort('checked') ?></th> 
       <th scope="col"><?= $this->Paginator->sort('id') ?></th> 
       <th scope="col"><?= $this->Paginator->sort('question') ?></th> 
       <th scope="col"><?= $this->Paginator->sort('marks') ?></th> 
       <th scope="col" class="actions"><?= __('Actions') ?></th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php foreach ($questions as $question): ?> 
      <tr> 

       <td><?= $this->Form->control('', ['type' => 'checkbox']) ?></td> 
       <td><?= $this->Number->format($question->id) ?></td> 
       <td><?= h($question->question) ?></td> 
       <td><?= $this->Number->format($question->marks) ?></td> 
       <td class="actions"> 
        <?= $this->Html->link(__('View'), ['action' => 'view', $question->id]) ?> 
        <?= $this->Html->link(__('Edit'), ['action' => 'edit', $question->id]) ?> 
        <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $question->id], ['confirm' => __('Are you sure you want to delete # {0}?', $question->id)]) ?> 
       </td> 
      </tr> 
      <?php endforeach; ?> 
     </tbody> 
    </table> 
    <div class="paginator"> 
     <ul class="pagination"> 
      <?= $this->Paginator->first('<< ' . __('first')) ?> 
      <?= $this->Paginator->prev('< ' . __('previous')) ?> 
      <?= $this->Paginator->numbers() ?> 
      <?= $this->Paginator->next(__('next') . ' >') ?> 
      <?= $this->Paginator->last(__('last') . ' >>') ?> 
     </ul> 
     <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p> 
    </div> 
    <?= $this->Form->button(__('Submit'), ['controller' => 'QuestionQuizzes', 'action' => 'index', $this->request->pass[1]]) ?> 
    <?= $this->Form->end() ?> 
</div> 

그리고 내 컨트롤러입니다.

<?php 
namespace App\Controller; 

use App\Controller\AppController; 

/** 
* Questions Controller 
* 
* @property \App\Model\Table\QuestionsTable $Questions 
* 
* @method \App\Model\Entity\Question[] paginate($object = null, array $settings = []) 
*/ 
class QuestionsController extends AppController 
{ 

    /** 
    * Index method 
    * 
    * @return \Cake\Http\Response|void 
    */ 
    public function index() 
    { 
     $questions = $this->paginate($this->Questions); 

     $this->set(compact('questions')); 
     $this->set('_serialize', ['questions']); 
    } 

    /** 
    * View method 
    * 
    * @param string|null $id Question id. 
    * @return \Cake\Http\Response|void 
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
    */ 
    public function view($id = null) 
    { 
     $question = $this->Questions->get($id, [ 
      'contain' => ['QuestionQuizzes'] 
     ]); 

     $this->set('question', $question); 
     $this->set('_serialize', ['question']); 
    } 

    /** 
    * Add method 
    * 
    * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. 
    */ 
    public function add() 
    { 
     $question = $this->Questions->newEntity(); 
     if ($this->request->is('post')) { 
      $question = $this->Questions->patchEntity($question, $this->request->getData()); 
      if ($this->Questions->save($question)) { 
       $this->Flash->success(__('The question has been saved.')); 

       return $this->redirect(['action' => 'index']); 
      } 
      $this->Flash->error(__('The question could not be saved. Please, try again.')); 
     } 
     $this->set(compact('question')); 
     $this->set('_serialize', ['question']); 
    } 

    /** 
    * Edit method 
    * 
    * @param string|null $id Question id. 
    * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. 
    * @throws \Cake\Network\Exception\NotFoundException When record not found. 
    */ 
    public function edit($id = null) 
    { 
     $question = $this->Questions->get($id, [ 
      'contain' => [] 
     ]); 
     if ($this->request->is(['patch', 'post', 'put'])) { 
      $question = $this->Questions->patchEntity($question, $this->request->getData()); 
      if ($this->Questions->save($question)) { 
       $this->Flash->success(__('The question has been saved.')); 

       return $this->redirect(['action' => 'index']); 
      } 
      $this->Flash->error(__('The question could not be saved. Please, try again.')); 
     } 
     $this->set(compact('question')); 
     $this->set('_serialize', ['question']); 
    } 

    /** 
    * Delete method 
    * 
    * @param string|null $id Question id. 
    * @return \Cake\Http\Response|null Redirects to index. 
    * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 
    */ 
    public function delete($id = null) 
    { 
     $this->request->allowMethod(['post', 'delete']); 
     $question = $this->Questions->get($id); 
     if ($this->Questions->delete($question)) { 
      $this->Flash->success(__('The question has been deleted.')); 
     } else { 
      $this->Flash->error(__('The question could not be deleted. Please, try again.')); 
     } 

     return $this->redirect(['action' => 'index']); 
    } 

    public function existingQuestion() 
    { 
     $questions = $this->paginate($this->Questions); 
     $quest = $this->Questions->newEntity(); 

     if ($this->request->is('post')) 
     { 
      $quest = $this->Questions->patchEntity($quest, $this->request->getData()); 
      if ($this->Questions->save($quest)) { 
       $this->Flash->success(__('The question has been saved.')); 

       return $this->redirect(['controller' => 'QuestionQuizzes', 'action' => 'index', $this->request->pass[1]]); 
      } 
      $this->Flash->error(__('The question could not be saved. Please, try again.')); 
     } 
     $this->set(compact('quest')); 
     $this->set('_serialize', ['quest']); 

     $this->set(compact('questions')); 
     $this->set('_serialize', ['questions']); 
    } 
} 

문제는 기존의 쿼리 기능에 있다고 생각합니다. 색인을 병합하고 동일한 기능에서 작업을 추가하는 올바른 방법을 모르겠습니다. 나는 여러 가지 방법을 시도했지만 여전히 실패했다. 미리 감사드립니다.

EDITED : 테이블 질문이 있는데이 테이블에서 데이터를 가져 오려는 양식을 만들고 싶습니다. 그런 다음 ID 옆에 여러 개의 확인란을 추가해야합니다. 나는 많은 방법들을 시도해 왔지만 그것이 옳지 않은 것처럼 보인다. I want the form to be like this

+0

여기서 무엇을 하려는지 완전히 명확하지 않습니다. 체크 박스 입력에 이름을 입력하지 않았으므로 게시 된 데이터에 어떤 내용이 포함될 것으로 예상됩니까? –

+0

나는이 포스트를 편집했다. 인덱스보기에서이 양식을 원하며 확인란을 추가해야합니다. 사용자는 여러 개의 확인란을 클릭 할 수 있습니다. 나는 이것을 설명하는 방법을 모른다. :( –

답변

1

선택한 질문의 ID를 가져오고 싶습니다. 데이터를 얻으려면,

$this->Form->control('questions[]', ['type' => 'checkbox', 'value' => $question->id]) 

같은 것을 시도 귀하의 $this->request->getData() 다음 선택한 ID의 목록 질문라는 배열이 있어야합니다.

데이터가 사용자의 existingQuestion 방법에 게시 될 때 다음 과제는 현재 질문이 새로운 질문을 만들려고 시도하는 것이므로 실제 질문은 기존 질문에 추가하는 것입니다. 놀리다. (이 방법의 이름은 addQuestions이어야하며 대신 퀴즈 컨트롤러에 있어야합니다.) 퀴즈에 질문을 추가하려면 Associating Many To Many Records을 읽어보십시오.