合并索引并将动作添加到cakephp 3.5的表单中 [英] Merge index and add action into form in cakephp 3.5

查看:69
本文介绍了合并索引并将动作添加到cakephp 3.5的表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cakephp的初学者,现在我正在使用cakephp 3.5。我需要创建表单时遇到问题。我要创建的表单来自索引视图,并且具有多个复选框。我的问题是,每当我单击复选框时,数据都不会保存。

I'm a beginner to cakephp and now I'm using cakephp 3.5. I'm having a problem when I need to create a form. The form that I want to create is from index view and it have multiple checkbox. My problem is whenever I click the checkbox, the data did not save.

表单的名称为exist_question.ctp。这是代码。

The form's name is existing_question.ctp. Here is the code.

<?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']);
    }
}

我认为问题出在现存的问题函数上。我不知道在同一函数中合并索引和添加操作的正确方法。我尝试了很多方法,但还是失败了。预先感谢。

I think the problem is at existingQuestion function. I don't know the correct way to merge index and add action in same function. I tried so many ways but it still failed. Thanks in advance.

编辑:
我有表问题,我想要创建的表单从该表中获取数据。然后,我需要在ID旁边添加多个复选框。我尝试了很多方法,但似乎不正确。 我希望表单像这样

推荐答案

您是否想获取所选问题的ID?要获取该数据,请尝试类似

Looks like you want to get the IDs of the selected questions? To get that data, try something like

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

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

Your $this->request->getData() should then have an array called questions with the list of selected IDs.

将这些数据发布到您的 existingQuestion 方法时,您将面临的下一个挑战是,因为目前正在尝试创建一个新问题,您真正想要的是在现有测验中添加问题。 (该方法可能应该命名为 addQuestions 并在测验控制器中?)要将问题添加到测验中,请阅读关联多对多记录

Your next challenge will be figuring out to do when that data is posted to your existingQuestion method, because what's there right now is trying to create a new question, when what it seems you really want is to add questions to an existing quiz. (The method should probably be named addQuestions and be in the quizzes controller instead?) To add the questions to the quiz, read up on Associating Many To Many Records.

这篇关于合并索引并将动作添加到cakephp 3.5的表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆