I made a little tweak for the Adam function to enable plugins:
public function modelExists($modelClass, $plugin=null){ $object = 'model'; if($plugin){ $object = $plugin.'.'.$object; } $model_list = array_flip(App::objects($object)); return isset($model_list[$modelClass]); }
One problem remains. It does not seem to pick up the models in the Lib / Model directory.
Failed to resolve this. The post will be updated if I find a solution.
I just added lib directory support
I also combined the plugin and modelClass cards (can be named as an array or string for convenience)
Here is a new feature:
public function modelExists($modelClass, $checkLoaded=true){ $modelClass = !is_array($modelClass)?$modelClass:implode('.', $modelClass);//implode if is array list($plugin, $modelClass) = pluginSplit($modelClass, true); $plugin=rtrim($plugin,'.'); $object = 'model'; if($plugin){ if($checkLoaded){ if(!CakePlugin::loaded($plugin)){ return false; } } $object = $plugin.'.'.$object; $libPaths = App::path("Lib/Plugin/$plugin"); } else { $libPaths = App::path('Lib'); } $list = App::objects($object, null, false); foreach($libPaths as $path){ $libModels = App::objects('lib.'.$object, $path.'Model'.DS, false ); if(is_array($libModels)){ $list = Hash::merge($list, $libModels); } } if(in_array($modelClass, $list)){ return true; } return false; }
Using:
it can be called like this:
$ this-> modelExists ('SomeModel');
$ this-> modelExists ('Plugin.SomeModel');
$ this-> modelExists (['SomeModel']);
$ this-> modelExists (['Plugin', 'SomeModel']);
// Pay attention to using array () instead of [] if you plan to deploy in php version <5.4