CakePHP 3.5保存到另一个表中

时间:2017-12-28 08:02:52

标签: php cakephp cakephp-3.0

我在cakephp 3.5中非常新,我遇到了将数据保存到不同表中的问题。我有问题表,并希望使用复选框功能将数据保存到QuestionQuizzes表中。当我尝试提交表单时,它会收到错误提示'问题无法保存"。

QuestionController:

public function existingQuestion()
{  

    $this->loadModel('QuestionsQuizzes');
    $questions = $this->paginate($this->Questions);
    $questionsQuiz = $this->QuestionsQuizzes->newEntity();

    if($this->request->is('post'))
    {

        $question = $this->QuestionsQuizzes->patchEntity($questionsQuiz, 
        $this->request->getData());

        if ($this->Questions->save($questionsQuiz)) {
            $this->Flash->success(__('The question has been saved.'));

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

    }

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

我只需将已检查的数据保存到数据库表中即可。

被修改: 所以我在我的ctp页面中放置了用于测验ID的隐藏文件,因为它似乎在数组中缺少测验ID。但当我把隐藏的字段并尝试调试它时,我得到this错误。

existing_question.ctp

      <form method="post" action="">
        <?php echo $this->Form->create($question) ?>
        <?php foreach ($questions as $question): ?>
        <tr>
          <td>
            <?= $this->Form->checkbox('id[].', array('value' => $question['id'], 'name' => 'Question[id][]', 'checked'=>false, 'hiddenField'=>false))?>
          </td>
          <td>
            <?= h($question->question) ?>
          </td>
          <td>
            <?= $this->Number->format($question->marks) ?>
          </td>
          <td>
            <?= $this->Form->hidden('quiz_id', ['value' => $this->request->pass[2]]);?>
          </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
  </table>
  <?= $this->Form->button(__('Submit'), ['action' => 'existingQuestion', $this->request->pass[2]]) ?>
    </form>
    <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>
      <?php this->Form->submit('Save'); ?>
    </div>
</fieldset>

然后我想也许我应该在QuizController中执行代码。我做到了,但是分页器出错了。

0 个答案:

没有答案