我无法使工作install()函数在自定义模块openart


I cannt make work install() function in custom module opencart

我正在制作openart自定义模块,现在我试图在模块安装创建DB表。当我安装模块时,它没有给出任何错误,模块正在工作,但在DB中不存在表。

一开始我试图使用模型

public function install() {
    $this->load->model('module/collectionfilter');
    $this->model_module_collectionfilter->createCollectionfilter();
 }

但是它不工作,所以我做了一点改变,使它更简单

        <?php
    class ControllerModuleCollectionfilter extends Controller {
     private $error = array();
   public function install() {
    $this->db->query("CREATE TABLE IF NOT           EXISTS`".DB_PREFIX."collectionfilter` (
                      `category_id` int(11) NOT NULL,
                      `attr_name` VARCHAR(45) NOT NULL,
                      PRIMARY KEY (`attr_name`)
                      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;");
   }

但它仍然不工作,我试图使它在phpMyAdmin,与

  CREATE TABLE IF NOT EXISTS `oc_collectionfilter` (
                      `category_id` int(11) NOT NULL,
                      `attr_name` VARCHAR(45) NOT NULL,
                      PRIMARY KEY (`attr_name`)
                      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

和表被创建,所以我不知道为什么它失败,有人可以帮助我吗??

public function install() {
    //$this->load->model('module/collectionfilter');
    //$this->model_module_collectionfilter->createCollectionfilter();
    $string = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "collectionfilter` (
                      `category_id` int(11) NOT NULL,
                      `attr_name` VARCHAR(45) NOT NULL,
                      PRIMARY KEY (`attr_name`)
                      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
    $try = $this->db->query($string);
    echo $try;
}
public function uninstall() {
    $this->load->model('module/collectionfilter');
    $this->model_module_collectionfilter->dropCollectionfilter();
}
public function index() {
    $this->load->language('module/collectionfilter');
    $this->document->setTitle($this->language->get('heading_title'));
    $this->load->model('setting/setting');
    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
        $this->model_setting_setting->editSetting('collectionfilter', $this->request->post);
        $this->session->data['success'] = $this->language->get('text_success');
        $this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
    }
    $data['heading_title'] = $this->language->get('heading_title');
    $data['text_edit'] = $this->language->get('text_edit');
    $data['text_enabled'] = $this->language->get('text_enabled');
    $data['text_disabled'] = $this->language->get('text_disabled');
    $data['entry_status'] = $this->language->get('entry_status');
    $data['button_save'] = $this->language->get('button_save');
    $data['button_cancel'] = $this->language->get('button_cancel');
    if (isset($this->error['warning'])) {
        $data['error_warning'] = $this->error['warning'];
    } else {
        $data['error_warning'] = '';
    }
    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_home'),
        'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
    );
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_module'),
        'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
    );
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('heading_title'),
        'href' => $this->url->link('module/collectionfilter', 'token=' . $this->session->data['token'], 'SSL')
    );
    $data['action'] = $this->url->link('module/collectionfilter', 'token=' . $this->session->data['token'], 'SSL');
    $data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
    if (isset($this->request->post['collectionfilter_status'])) {
        $data['collectionfilter_status'] = $this->request->post['collectionfilter_status'];
    } else {
        $data['collectionfilter_status'] = $this->config->get('collectionfilter_status');
    }
    $data['header'] = $this->load->controller('common/header');
    $data['column_left'] = $this->load->controller('common/column_left');
    $data['footer'] = $this->load->controller('common/footer');
    $this->response->setOutput($this->load->view('module/collectionfilter.tpl', $data));
}
protected function validate() {
    if (!$this->user->hasPermission('modify', 'module/collectionfilter')) {
        $this->error['warning'] = $this->language->get('error_permission');
    }
    return !$this->error;
}
 }

解,它是。它是托管和我的合作伙伴,我在旧的DB工作,网站是不同的。

我刚刚尝试了你的安装功能,它工作良好,并在数据库中创建表。

登录到phpMyAdmin的隐身窗口,检查你的表是否存在。有时phpMyAdmin不显示新表,直到你使用它的刷新按钮。

控制器:admin/控制器/模块/collectionfilter.php

private $error = array();
public function install() {
    //$this->load->model('module/collectionfilter');
    //$this->model_module_collectionfilter->createCollectionfilter();
    $string = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "collectionfilter` (
                  `category_id` int(11) NOT NULL,
                  `attr_name` VARCHAR(45) NOT NULL,
                  PRIMARY KEY (`attr_name`)
                  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
    $try = $this->db->query($string);
    echo $try;
    die();
}
public function uninstall() {
    $this->load->model('module/collectionfilter');
    $this->model_module_collectionfilter->dropCollectionfilter();
}
public function index() {
    $this->load->language('module/collectionfilter');
    $this->document->setTitle($this->language->get('heading_title'));
    $this->load->model('setting/setting');
    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
        $this->model_setting_setting->editSetting('collectionfilter', $this->request->post);
        $this->session->data['success'] = $this->language->get('text_success');
        $this->response->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
    }
    $data['heading_title'] = $this->language->get('heading_title');
    $data['text_edit'] = $this->language->get('text_edit');
    $data['text_enabled'] = $this->language->get('text_enabled');
    $data['text_disabled'] = $this->language->get('text_disabled');
    $data['entry_status'] = $this->language->get('entry_status');
    $data['button_save'] = $this->language->get('button_save');
    $data['button_cancel'] = $this->language->get('button_cancel');
    if (isset($this->error['warning'])) {
        $data['error_warning'] = $this->error['warning'];
    } else {
        $data['error_warning'] = '';
    }
    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_home'),
        'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
    );
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_module'),
        'href' => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL')
    );
    $data['breadcrumbs'][] = array(
        'text' => $this->language->get('heading_title'),
        'href' => $this->url->link('module/collectionfilter', 'token=' . $this->session->data['token'], 'SSL')
    );
    $data['action'] = $this->url->link('module/collectionfilter', 'token=' . $this->session->data['token'], 'SSL');
    $data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
    if (isset($this->request->post['collectionfilter_status'])) {
        $data['collectionfilter_status'] = $this->request->post['collectionfilter_status'];
    } else {
        $data['collectionfilter_status'] = $this->config->get('collectionfilter_status');
    }
    $data['header'] = $this->load->controller('common/header');
    $data['column_left'] = $this->load->controller('common/column_left');
    $data['footer'] = $this->load->controller('common/footer');
    $this->response->setOutput($this->load->view('module/collectionfilter.tpl', $data));
}
protected function validate() {
    if (!$this->user->hasPermission('modify', 'module/collectionfilter')) {
        $this->error['warning'] = $this->language->get('error_permission');
    }
    return !$this->error;
}
}
?>

语言:admin/语言/英语/模块/collectionfilter.php

<?php
    // Heading
    $_['heading_title']       = 'Collection Filter';
    // Text
    $_['text_module']         = 'Modules';
    $_['text_success']        = 'Success: You have modified module Google Talk!';
    $_['text_content_top']    = 'Content Top';
    $_['text_content_bottom'] = 'Content Bottom';
    $_['text_column_left']    = 'Column Left';
    $_['text_column_right']   = 'Column Right';
    // Entry
    $_['entry_code']          = 'Google Talk Code:<br /><span class="help">Goto <a href="http://www.google.com/talk/service/badge/New" target="_blank"><u>Create a Google Talk chatback badge</u></a> and copy &amp; paste the generated code into the text box.</span>';
    $_['entry_layout']        = 'Layout:';
    $_['entry_position']      = 'Position:';
    $_['entry_status']        = 'Status:';
    $_['entry_sort_order']    = 'Sort Order:';
    // Error
    $_['error_permission']    = 'Warning: You do not have permission to modify module Google Talk!';
    $_['error_code']          = 'Code Required';
    ?>

模板:admin/视图/模板/模块/collectionfilter.tpl

            <?php echo $header; ?>
        <div id="content">
          <div class="breadcrumb">
            <?php foreach ($breadcrumbs as $breadcrumb) { ?>
            <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
            <?php } ?>
          </div>
          <?php if ($error_warning) { ?>
          <div class="warning"><?php echo $error_warning; ?></div>
          <?php } ?>
          <div class="box">
            <div class="heading">
              <h1><img src="view/image/module.png" alt="" /> <?php echo $heading_title; ?></h1>
              <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
            </div>
            <div class="content">
              <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
                <table id="module" class="list">
                  <thead>
                    <tr>
                      <td class="left"><?php echo $entry_layout; ?></td>
                      <td class="left"><?php echo $entry_position; ?></td>
                      <td class="left"><?php echo $entry_status; ?></td>
                      <td class="right"><?php echo $entry_sort_order; ?></td>
                      <td></td>
                    </tr>
                  </thead>
                  <?php $module_row = 0; ?>
                  <?php foreach ($modules as $module) { ?>
                  <tbody id="module-row<?php echo $module_row; ?>">
                    <tr>
                      <td class="left"><select name="filter_module[<?php echo $module_row; ?>][layout_id]">
                          <?php foreach ($layouts as $layout) { ?>
                          <?php if ($layout['layout_id'] == $module['layout_id']) { ?>
                          <option value="<?php echo $layout['layout_id']; ?>" selected="selected"><?php echo $layout['name']; ?></option>
                          <?php } else { ?>
                          <option value="<?php echo $layout['layout_id']; ?>"><?php echo $layout['name']; ?></option>
                          <?php } ?>
                          <?php } ?>
                        </select></td>
                      <td class="left"><select name="filter_module[<?php echo $module_row; ?>][position]">
                          <?php if ($module['position'] == 'content_top') { ?>
                          <option value="content_top" selected="selected"><?php echo $text_content_top; ?></option>
                          <?php } else { ?>
                          <option value="content_top"><?php echo $text_content_top; ?></option>
                          <?php } ?>
                          <?php if ($module['position'] == 'content_bottom') { ?>
                          <option value="content_bottom" selected="selected"><?php echo $text_content_bottom; ?></option>
                          <?php } else { ?>
                          <option value="content_bottom"><?php echo $text_content_bottom; ?></option>
                          <?php } ?>
                          <?php if ($module['position'] == 'column_left') { ?>
                          <option value="column_left" selected="selected"><?php echo $text_column_left; ?></option>
                          <?php } else { ?>
                          <option value="column_left"><?php echo $text_column_left; ?></option>
                          <?php } ?>
                          <?php if ($module['position'] == 'column_right') { ?>
                          <option value="column_right" selected="selected"><?php echo $text_column_right; ?></option>
                          <?php } else { ?>
                          <option value="column_right"><?php echo $text_column_right; ?></option>
                          <?php } ?>
                        </select></td>
                      <td class="left"><select name="filter_module[<?php echo $module_row; ?>][status]">
                          <?php if ($module['status']) { ?>
                          <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                          <option value="0"><?php echo $text_disabled; ?></option>
                          <?php } else { ?>
                          <option value="1"><?php echo $text_enabled; ?></option>
                          <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                          <?php } ?>
                        </select></td>
                      <td class="right"><input type="text" name="filter_module[<?php echo $module_row; ?>][sort_order]" value="<?php echo $module['sort_order']; ?>" size="3" /></td>
                      <td class="left"><a onclick="$('#module-row<?php echo $module_row; ?>').remove();" class="button"><?php echo $button_remove; ?></a></td>
                    </tr>
                  </tbody>
                  <?php $module_row++; ?>
                  <?php } ?>
                  <tfoot>
                    <tr>
                      <td colspan="4"></td>
                      <td class="left"><a onclick="addModule();" class="button"><?php echo $button_add_module; ?></a></td>
                    </tr>
                  </tfoot>
                </table>
              </form>
            </div>
          </div>
        </div>
        <script type="text/javascript"><!--
        var module_row = <?php echo $module_row; ?>;
        function addModule() {  
                html  = '<tbody id="module-row' + module_row + '">';
                html += '  <tr>';
                html += '    <td class="left"><select name="filter_module[' + module_row + '][layout_id]">';
                <?php foreach ($layouts as $layout) { ?>
                html += '      <option value="<?php echo $layout['layout_id']; ?>"><?php echo addslashes($layout['name']); ?></option>';
                <?php } ?>
                html += '    </select></td>';
                html += '    <td class="left"><select name="filter_module[' + module_row + '][position]">';
                html += '      <option value="content_top"><?php echo $text_content_top; ?></option>';
                html += '      <option value="content_bottom"><?php echo $text_content_bottom; ?></option>';
                html += '      <option value="column_left"><?php echo $text_column_left; ?></option>';
                html += '      <option value="column_right"><?php echo $text_column_right; ?></option>';
                html += '    </select></td>';
                html += '    <td class="left"><select name="filter_module[' + module_row + '][status]">';
            html += '      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
            html += '      <option value="0"><?php echo $text_disabled; ?></option>';
            html += '    </select></td>';
                html += '    <td class="right"><input type="text" name="filter_module[' + module_row + '][sort_order]" value="" size="3" /></td>';
                html += '    <td class="left"><a onclick="$(''#module-row' + module_row + ''').remove();" class="button"><?php echo $button_remove; ?></a></td>';
                html += '  </tr>';
                html += '</tbody>';
                $('#module tfoot').before(html);
                module_row++;
        }
        //--></script> 
        <?php echo $footer; ?>