从default.ctp访问静态方法

isw*_*wan 0 cakephp cakephp-2.0

class Cat extend AppModel{
   public static function getCat($medium=NULL){
       $allcat = $this->Cat->find('all', array('contain' =>false,
                'conditions' => array('Cat.c_medium' => $medium), 
                 'order' => array('Cat.c_name' => 'asc')));
       return $allcat;
   }
}
Run Code Online (Sandbox Code Playgroud)

我想从default.ctp中访问此方法,Cat::getCat('eng'); 但这不起作用,请帮助我.

错误: - Fatal error: Class 'Cat' not found in C:\xampp\htdocs\app\View\Layouts\default.ctp on line 100

mar*_*ark 6

您需要App :: uses()要在文件中使用的类.对于视图,通常最好在最顶层的控制器中执行此操作:

 <?php
     App::uses('Cat', 'Model');
Run Code Online (Sandbox Code Playgroud)

现在可以在所有控制器操作及其视图中访问Cat模型 - 尤其是通过静态访问(对于非静态访问,ClassRegistry :: init()通常会处理包含本身).

但在你的情况下,你在这里滥用静态方法.您应该只为非查询方法静态访问模型.

这里的方法首先应该是静态的.使用控制器调用此方法并将结果传递给视图.