【问题标题】:Implement tables with error underscores: Fatal error: Call to a member function实现带有错误下划线的表:致命错误:调用成员函数
【发布时间】:2019-11-29 10:46:43
【问题描述】:

当我实现一个名为 example_a 的表时:

  • 模型/实体:ExampleA.php
  • 模型/表格:ExampleATable.php
  • 控制器:ExampleAController.php
  • 模板/示例A:index.ctp

给我以下错误:

注意 (1024):未定义的属性:ExampleAController :: $ ExampleA in C: Program Files ...

致命错误:在布尔值上调用成员函数 find()

打印控制器中的对象列表实现一个函数:

 public function index ()
 {
     $This->set('examples',$this->ExampleA->find('all'));
 }

我指定此表与其他表没有任何关系

如果我做同样的事情来实现一个不带下划线的表格,它会起作用

【问题讨论】:

标签: php cakephp cakephp-3.0 camelcasing cakephp-3.8


【解决方案1】:

这里的主要问题是您偏离了CakePHP conventions,这很好,但意味着您需要做一些额外的工作。

要做的第一件事是告诉框架你的 Table 和 Entity 类被称为什么。在你的ExampleATable.php文件中initialize()方法中,需要设置表、实体等东西。

<?php
public function initialize(array $config)
{
    $this->setTable('example_a');
    $this->setAlias('ExampleA');
    $this->setEntityClass(\App\Model\Entity\ExampleA::class);
    //etc

其次,在您的控制器中,我们需要手动加载 Table 类,因为它与控制器不匹配。

// Get or create a table instance
$ExampleATable = $this->getTableLocator()->get('ExampleA');

// Use the table instance to query
$query = $ExampleATable->find();

【讨论】:

  • 我已经阅读了 CakePHP 约定。我以为我尊重他们,也许唯一的就是模型/实体名称。像我应该做的那样尊重他们?
猜你喜欢
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 2012-09-29
  • 2014-02-05
  • 2014-04-29
  • 2015-09-14
  • 2014-10-07
相关资源
最近更新 更多