0

我一直在关注 Samuel Georges 关于此事的教程:https ://octobercms.com/support/article/ob-21 。但是,在我的用例中,我有这些模型:

  • 基本章节模型 ( Acme\Formist1\Models\Chapter)
  • 属于章节的测验 ( Acme\Formist1\Models\Quiz)
  • 属于测验的问题 ( Acme\Formist1\Models\Question)
  • 和属于问题的答案 ( Acme\Formist2\Models\Answer)

我需要更深一层,所以我onCreateItem()稍微修改了方法:

    public function onCreateRecord()
    {
        $recordType = post('record_type');

        $data = $recordType == 'question'
            ? $this->questionFormWidget->getSaveData()
            : $this->answerFormWidget->getSaveData();

        $model = $recordType == 'question'
            ? new Question
            : new Answer;
        $model->fill($data);
        $model->save();

        $parent = $this->getParentModel($recordType);

        if($recordType == 'question') {
            $parent->questions()->add($model, $this->questionFormWidget->getSessionKey());
        }
        else{
            $parent->answers()->add($model, $this->answerFormWidget->getSessionKey());
        }

        return $this->refreshRecordList($recordType);
    }

基本上,我为 Answers 创建了另一个表单,但是该处理程序发生了一些奇怪的事情:延迟 Question 模型的关系绑定没有问题,但是每当我尝试验证 Answer 表单时,我都会收到一条错误消息,告诉我foreign_key 没有默认值,因此暗示 Answer 模型没有任何延迟绑定。

另外,如果您对此功能有更好的建议,请随时提交,我知道这个看起来不漂亮。

提前谢谢你的时间。

4

0 回答 0