【问题标题】:Listing Form Templates within another Controller view在另一个控制器视图中列出表单模板
【发布时间】:2018-09-18 03:55:26
【问题描述】:

我有一个 form_templates 表和一个 forms 表。它们通过 form_template_id 连接。我希望能够在我在 Forms 控制器中创建的 select.ctp 文件中列出按标题创建的 form_templates。只是想知道如何用 cakephp 做到这一点?

目前我的 FormsController 中有以下代码:

    public function select()
{
    $this->set('page_heading', 'Current Forms');

    $contain = [];

    $formTemplate = $this->FormTemplates->Forms->find('list', ['order' => 'title'])->where(['active'=>true]);

    $forms = $this->paginate($this->Forms->FormTemplates);
    $this->set(compact('forms', 'formTemplate'));
}

但我在 null 错误时收到对成员函数 find() 的调用。

任何有关如何解决此问题的帮助将不胜感激。我知道这很简单,但我是 cakephp 新手。

【问题讨论】:

  • 在调用$formTemplate = $this->FormTemplates->Forms->find('list', ['order' => 'title'])->where(['active'=>true]);之前加载模型($this->loadModel('FormTemplates'))。
  • 感谢@Ishanvyas22,但我仍然收到相同的错误消息。

标签: cakephp orm cakephp-3.0


【解决方案1】:

在您的FormsController 中,只有FormsTable 会自动加载,而您正在尝试访问当前未加载的模型:

$formTemplate = $this->FormTemplates->Forms->find(...

要得到你想要的,你应该像这样访问关联的FormTemplatesTable

$formTemplate = $this->Forms->FormTemplates->find(...

【讨论】:

  • 谢谢希蒙。我已经添加了这个,但仍然有同样的错误。两个表都通过内部连接链接。我已经设置了由以下组成的 select.ctp 表: form_templates)): ?>
    和 tbody 以:form_template as $formTemplates): ?> 开头
  • 如果您仍然有此错误,请与完整的堆栈跟踪共享完整的错误文本。
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
相关资源
最近更新 更多