2016-07-27 16 views
0

Ich versuche, eine vollständige Liste aller Modelle unter app/Model zu erhalten.Get Liste der Modelle in CakePHP 2

Bereits versucht App::objects('Model'), aber es ruft nur geladene Modelle ab.

Ist dies in CakePHP 2 möglich?

+0

mit Kuchen 2.8 es alle Modelle gibt, nicht nur die geladenen diejenigen – arilia

Antwort

0

Nach einigen Recherchen fand ich, dass App::objects('Model') alle Modelle unter app/Models zurückgibt und Plugin-Modelle nicht enthält.

Um alle Modelle (App Modelle und Plugin-Modelle) habe ich die nächste Funktion:

/** 
* Get models list. 
* 
* @return array 
*/ 
public static function getModels() { 

    // Get app models 
    $models = App::objects('Model'); 

    $models = array_combine($models, $models); 

    // Get plugins models 
    $pluginsFolder = new Folder(APP . 'Plugin'); 

    $plugins = $pluginsFolder->read(); 

    foreach ($plugins[0] as $plugin) { 

     $pluginModels = App::objects($plugin . '.Model'); 

     foreach ($pluginModels as $pluginModel) { 

      $models[$plugin . '.' . $pluginModel] = $plugin . '.' . $pluginModel; 

     } 

    } 

    // Exclude tableless and application models 
    $dataSource = ConnectionManager::getDataSource('default'); 

    $sources = $dataSource->listSources(); 

    foreach($models as $key => $model) { 

     $table = Inflector::tableize(self::modelName($key)); 

     if (stripos($model, 'AppModel') > -1 || !in_array($table, $sources)) { 

      unset($models[$key]); 

     } 

    } 

    return $models; 

}