错误:对cakephp控制器中的非对象调用成员函数find()

错误:对cakephp控制器中的非对象调用成员函数find(),php,cakephp,Php,Cakephp,我是cakephp新手,尝试使用控制台工具生成一些CRUD操作。除了一张桌子(最大的)外,它工作正常 尝试添加新元素时,将返回: 错误:对非对象调用成员函数find() 文件:C:\wamp\www\cakephp\app\Controller\ChantiersController.php 电话号码:50 这是第50行及其后的内容: $programs = $this->Chantier->Program->find('list'); $etats = $th

我是cakephp新手,尝试使用控制台工具生成一些CRUD操作。除了一张桌子(最大的)外,它工作正常

尝试添加新元素时,将返回:

错误:对非对象调用成员函数find() 文件:C:\wamp\www\cakephp\app\Controller\ChantiersController.php
电话号码:50

这是第50行及其后的内容:

    $programs = $this->Chantier->Program->find('list');
    $etats = $this->Chantier->Etat->find('list');
    $types = $this->Chantier->Type->find('list');
    $champsLibres = $this->Chantier->ChampsLibre->find('list');
    $feuillesDeRoutes = $this->Chantier->FeuillesDeRoute->find('list');
    $directionsPilotes = $this->Chantier->DirectionsPilote->find('list');
    $this->set(compact('programs', 'etats', 'types', 'champsLibres',    
            'feuillesDeRoutes', 'directionsPilotes'));

TLDR/回答:

您可以修复关联,也可以加载模型,然后删除直通调用(Chantier部分:

$this->loadModel('Program');
$programs = $this->Program->find('list');
详细信息/说明:

该错误基本上意味着您试图对控制器中未加载的模型调用
find()

默认情况下,控制器的模型是加载的。并且,当您这样做时,您可以通过加载的模型使用模型。(如果关联设置正确)

例如:

//ChantiersController
$this->Pizza->find('all'); //ERROR - Pizza model isn't loaded
要解决此问题,请在尝试使用模型之前加载模型:

$this->loadModel("Pizza");
$this->Pizza->find('all'); //All is good - loaded on the line above

在您的情况下,由于您似乎正在使用与
Chantier
模型的关联来查找其他模型,因此这两个模型之间的关联可能不正确。

如果您不想使用loadModel()函数,则可以定义

var $uses = array('Program');

您还可以在数组中提到其他模型名称

请显示
var_dump($this->Chantier->Program)的输出;
您可以包括第49行以防万一吗?如果没有看到您的关联,这是不可能的。可能需要开始一个新问题“模型关联不起作用”或类似的问题。