CakePHP 3: исключить таблицы поставщиков при получении списка всех объектов таблицы - PullRequest
0 голосов
/ 12 июня 2018

в моей базе данных, у меня есть таблицы, созданные некоторыми плагинами.Мне нужно показать только мои модели в раскрывающемся списке.

в 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);
}

В индексе.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)
        ));

Мой результат показывает все таблицы.Где моя ошибка?

1 Ответ

0 голосов
/ 12 июня 2018

Вы звоните 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);
}
...