合并索引并在cakephp 3.5中将操作添加到表单中

时间:2017-11-30 19:45:54

标签: php cakephp cakephp-3.x

我是cakephp的初学者,现在我正在使用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

1 个答案:

答案 0 :(得分:1)

您希望获得所选问题的ID吗?要获取该数据,请尝试类似

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

您的$this->request->getData()应该有一个名为问题的数组,其中包含所选ID列表。

当您将数据发布到existingQuestion方法时,您的下一个挑战就是要做好准备,因为现在正在尝试创建一个新问题,当您真正想要的时候是向现有测验添加问题。 (该方法可能应该命名为addQuestions而不是在测验控制器中?)要将问题添加到测验中,请阅读Associating Many To Many Records