ppController中为cakephp调用AppModel函

ppController中为cakephp调用AppModel函

本文介绍了在AppController中为cakephp调用AppModel函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我希望所有控制器都能使用的功能,所以我已经在AppController中定义了它。现在,此函数将要执行的操作的一部分与控制器无关,因此应该在模型中,但是由于这是通用操作,因此在AppModel中看起来是正确的。我的函数如下所示:

I have a function that I want all of my controllers to be able to use, so I have defined it in AppController. Now part of what this function will do has no business being in a controller and so it should be in a model, but since this is a universal operation, it only seems correct that it be in AppModel. My function looks as followed:

class AppController extends Controller {

    public function i_need_this_everywhere ($term) {
        // do some stuff

        // this is the line that is an issue
        // it seems like this should be simple and work, but no variation of this is working
        $value = $this->App->get_some_cool_data($term);

        return $value;
    }

}

我只是想能够从AppController调用AppModel函数。我尝试了以下方法:

I simply want to be able to call an AppModel function from AppController. I have tried the following:

// I have tried several variations of this.
$this->loadModel('AppModel');
$this->AppModel->get_some_cool_data($term);

但这会抱怨缺少名为AppModel的数据库表,这时我尝试在AppModel中设置:

But this then complains about the lack of database table called AppModel, at which point, in AppModel I tried setting:

public $useTable = FALSE;

但是这会使整个应用程序崩溃,因此...现在我没有主意了。任何帮助将不胜感激。

But that blows the whole app up so... Now I am out of ideas. Any help would be greatly appreciated.

推荐答案

所有模型都应继承自AppModel,为什么不调用 $ this-> AnyModel- > get_some_cool_data($ term); 代替?无论如何,AppModel应该几乎是一个抽象类-您几乎永远都不想实例化它,而只是将其用作基类。

All your models should inherit from AppModel, so why not call $this->AnyModel->get_some_cool_data($term); instead? AppModel should be pretty much be an abstract class anyways -- you pretty much never want to instantiate it, you just use as a base class.

此外,那应该是 $ useTable = false; 不是 $ usesTable 。再次提醒您,所有模型都应继承自AppModel,因此所有模型最终也将不使用数据库,这可能是错误的根源。

Also, that should be $useTable = false; not $usesTable. Remember that, again, all your models should inherit from AppModel, so all your models will end up not using the database as well, which is probably the source of your errors.

这篇关于在AppController中为cakephp调用AppModel函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:35