Merge index and add action into form in cakephp 3.5

家住魔仙堡 提交于 2019-12-11 15:22:17

问题


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.

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>

And this is my controller.

<?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.

EDITED: I have table question and I want the form that I want to create to get data from that table. Then, I need to add multiple checkbox next to id. I have tried so many ways but it seem not right. I want the form to be like this


回答1:


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])

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

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.



来源:https://stackoverflow.com/questions/47581458/merge-index-and-add-action-into-form-in-cakephp-3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!