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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11474447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:35:52  来源:igfitidea点击:

Error: Call to a member function find() on a non-object in cakephp controller

phpcakephp

提问by L. Sanna

I'm new to cakephp and tried to generate some CRUD operations with the console tool. It works fine except for one table (the biggest).

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

When trying to add a new element, it returns:

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

Error: Call to a member function find() on a non-object File: C:\wamp\www\cakephp\app\Controller\ChantiersController.php
Line: 50

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

Here is the line 50 and beyond:

这是第 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'));

回答by Dave

TLDR / Answer:

TLDR/答案:

You can either fix the association, or load the model, and remove the through-call (the ->Chantierportion:

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

$this->loadModel('Program');
$programs = $this->Program->find('list');

Details / Explanation:

详情/说明:

That error basically means you're trying to call find()on a model that isn't loaded in the controller.

该错误基本上意味着您正在尝试调用find()未加载到控制器中的模型。

By default, the Controller's model is loaded. And, as you're doing, you can use a model THROUGH a loaded model. (if the associations are set up correctly).

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

For example:

例如:

//ChantiersController
$this->Pizza->find('all'); //ERROR - Pizza model isn't loaded

To resolve this, just load the model prior to trying to use it:

要解决此问题,只需在尝试使用模型之前加载模型:

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

In your case, since it looks like you're using associations with the Chantiermodel to find through other models, it's likely the association between the two models is not correct.

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

回答by Shrish Shrivastava

If you don't want to use the loadModel() function then you can define

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

var $uses = array('Program');

You can also mention other model names also in the array

您还可以在数组中提及其他型号名称