私はcakephpの初心者ですが、今はcakephp 3.5を使用しています。私はフォームを作成する必要があるときに問題が発生しています。私が作成したいフォームは、インデックスビューからのもので、複数のチェックボックスがあります。私の問題は、チェックボックスをクリックするたびにデータが保存されなかったことです。indexをマージして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
ここでは、あなたがやろうとしていることは完全にはっきりしていません。チェックボックス入力に名前を入力していないので、投稿されたデータにはどのようなことが起こると思いますか? –
この投稿を編集しました。私はこのフォームをインデックスビューで表示したいので、チェックボックスを追加する必要があります。ユーザーは複数のチェックボックスをクリックできます。私はこれを説明する方法を知らない。 :( –