我会用App::objects('model')
作为2.x(不知道这是什么时候实现的)。
class AppController extents Controller {
private function _modelExists($modelName){
$models = App::objects('model');
return in_array($modelName,$models);
}
}
//Somewhere in your logic
if($this->_modelExists('SomeModel')){
//do model exists logic
} else {
//do other logic
}
*请注意,App::objects('model')
将不包含插件中的模型。你可以这样做:
$models = array_merge(
App::objects('model'),
App::objects('MyPlugin.model')
);
你也可以做到这一点与纯PHP如下
if(class_exists('SomeModel')){
//do model exists logic
} else {
//do other logic
}
// The pitfall of this approach, is that it will not assure
// that `SomeModel is a decedent of the `Model` class.
实际上它从来没有空! 其AppModel如果无法找到该类。 你需要检查App :: import(),它返回一个成功的布尔值 – mark
嗨马克,谢谢你做到了。 – Manu
错了,这个init初学了一个新类,反正它与你声明的模型不匹配 –