2016-07-27 2 views
0

app/Modelの下にすべてのモデルの完全なリストを取得しようとしています。CakePHP 2のモデルリストを取得

すでにApp::objects('Model')を試しましたが、ロードされたモデルのみを取得します。

これはCakePHP 2で可能ですか?

+0

ケーキ2.8を使用して、それはすべてのモデルだけでなく、ロードされたものを与える:

は、私は次の関数を作成したすべてのモデル(アプリモデルやプラグインモデル)を含むように – arilia

答えて

0

若干の調査の結果、App::objects('Model')はすべてapp/Modelsのモデルを返しますが、プラグインモデルは含まれていません。

/** 
* 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; 

} 
関連する問題