How to check if a model exists with CakePHP?

I dynamically load models into a general purpose function, and I noticed that sometimes I want to skip loading models because it causes a 404 error.

How to check if a model exists?

Sort of:

if($this->modelexists($type) { $this->loadModel($type); } else { return "xxx"; } 
+4
source share
5 answers

Since you did not specify your version, I split my answer into two, one for 1.3 and one for 2.0.

CakePHP 1.3

The loadModel() method returns false if it cannot find the model, see the API documentation. So just make sure it doesn't return false:

 if(!$this->loadModel($type)) { return "xxx"; } 

CakePHP 2.0

If the model class does not exist, the loadModel() method will raise a MissingModelException , so just catch it.

See the API docs on this.

Example:

 try { $this->loadModel($type); } catch(MissingModelException $e) { // Model not found! echo $e->getMessage(); } 
+8
source

CakePHP 2.x

 function model_exists($type){ $model_list = array_flip(App::objects('model')); return isset($model_list[$type]); } 

You can add this to the AppController and use it in conjunction with and __ get () to automatically load the model if you want. In this case, you can use a member variable (for example, $ this-> model_list) to save the list, so you do not need to call App :: objects () every time.

+3
source

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

+1
source

for 1.3 you can use

 App::import('Model', 'ModelName'); 

which returns false if it does not exist

For 2.x, I have not yet found a working solution

0
source

it works for me as for pie 2

 if (in_array($model, App::objects('Model'))) { $this->loadModel($model); } else { return 'error'; } 

from App::objects('Model') it returns all model names as an array

0
source

Source: https://habr.com/ru/post/1397651/


All Articles