cakephp - CakePHP 3:在获取所有表对象的列表时排除供应商表
问题描述
在我的数据库中,我有一些插件创建的表。我只需要在下拉列表中显示我的模型。
在 bootstrap.php 中:
Configure::write('ReportManager.modelIgnoreList',array(
'acl_phinxlog',
'acos',
'aros',
'aros_acos',
'audits',
'burzum_file_storage_phinxlog',
'burzum_user_tools_phinxlog',
'cake_d_c_users_phinxlog',
'file_storage',
'phinxlog',
));
在我的控制器索引函数中:
if (empty($this->data)) {
$modelIgnoreList = Configure::read('ReportManager.modelIgnoreList');
$models = ConnectionManager::get('default')->schemaCollection()->listTables();
foreach($models as $key => $model) {
if ( isset($modelIgnoreList) && is_array($modelIgnoreList)) {
foreach ($modelIgnoreList as $ignore) {
if (isset($models[$ignore])) {
unset($models[$ignore]);
$modelData = TableRegistry::get($model);
debug($modelData);
}
}
}
}
debug($modelIgnoreList);
}
在 index.ctp 中:
echo $this->Form->create('ReportManager');
echo '<fieldset>';
echo '<legend>' . __d('report_manager','New report',true) . '</legend>';
echo $this->Form->input('model',array(
'type'=>'select',
'label'=>__d('report_manager','Model',true),
'options'=>$models,
'empty'=>__d('report_manager','--Select--',true)
));
我的结果一直显示所有表格。我的错误在哪里?
解决方案
您正在unset
使用模型名称而不是密钥进行调用。此外,这里不需要两个foreach
循环。
$models = ConnectionManager::get('default')->schemaCollection()->listTables();
if (isset($modelIgnoreList) && is_array($modelIgnoreList)) {
foreach($models as $key => $model) {
if (in_array($model, $modelIgnoreList)) {
unset($models[$key]);
}
}
}
或者,更简单的是,使用内置功能为您处理此问题:
$models = ConnectionManager::get('default')->schemaCollection()->listTables();
if (isset($modelIgnoreList) && is_array($modelIgnoreList)) {
$models = array_diff($models, $modelIgnoreList);
}
推荐阅读
- android - 如何防止 ConcurrentModificationException
- macos - Delphi 10.3.3 macOS Notarization,错误客户端退出代码1
- python - Python 字典理解
- vb.net - 从 Form2 获取 Form1 复选框的值以构建 Exe
- python - 如何正确使用 GradientTape 在 TensorFlow 中制作自定义损失函数?
- android - 即使安装了应用,Firebase 动态链接也会重定向到 Playstore
- c - Unisys 最新的大型机系统是否仍然使用反码表示?
- c - Effective type rules with relation to strict aliasing
- javascript - how do I run code after the last element has completed its jQuery slideup effect?
- html - 如何在 ruby on rails 的 2 个字符串之间插入和变量