在CakePHP 2中获取模型列表

在CakePHP 2中获取模型列表,php,cakephp-2.0,Php,Cakephp 2.0,我正在尝试获取app/Model下所有型号的完整列表 已经尝试了App::objects('Model'),但它只检索加载的模型 这在CakePHP 2中可能吗?经过一些研究,我发现App::objects('Model')返回App/models下的所有模型,但它不包括插件模型 为了包含所有模型(应用程序模型和插件模型),我创建了下一个函数: /** * Get models list * * @return array */ public static function getMod

我正在尝试获取
app/Model
下所有型号的完整列表

已经尝试了
App::objects('Model')
,但它只检索加载的模型


这在CakePHP 2中可能吗?

经过一些研究,我发现
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;
}

经过一些研究,我发现
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;
}

也许晚了,但这是我的版本: (循环浏览插件模型,通过打开文件并搜索变量$useTable来检索关联的表)


也许晚了,但这是我的版本: (循环浏览插件模型,通过打开文件并搜索变量$useTable来检索关联的表)

CakePHP v2

在AppController中

if (in_array('YOUR_MODEL', $this->uses)) {
    //found
}
CakePHP v2

在AppController中

if (in_array('YOUR_MODEL', $this->uses)) {
    //found
}

使用cake 2.8,它提供所有模型,而不仅仅是加载的模型使用cake 2.8,它提供所有模型,而不仅仅是加载的onesHow,这会返回使用过的模型列表吗?$this->uses保存控制器中使用的模型数组。显然没有考虑插件中使用的模型。如果处理一个小项目,简单的黑客攻击。这如何返回已使用模型的列表?$this->uses保存控制器中使用的模型数组。显然没有考虑插件中使用的模型。简单的黑客,如果与一个小项目的工作。