Prestashop - 以下模块没有正确安装era_widget

时间:2014-08-19 04:22:14

标签: php prestashop

我尝试使用下面的代码创建一个新模块,但我得到一个错误,因为相同的标题。请帮我查一下!

<?php

if (!defined('_PS_VERSION_')) exit;

include_once(_PS_MODULE_DIR_ . 'era_widget/widgetModel.php');

/**
 * Description of era_widget
 *
 * @author nguye_000
 */
class era_widget extends Module {

    private $_html = '';

    function __construct() {

        $this->name = 'era_widget';
        $this->tab = 'other';
        $this->version = '1.0';
        $this->author = 'Nam Nguyen';
        $this->need_instance = 0;
        $this->secure_key = Tools::encrypt($this->name);
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Era Widget');
        $this->description = $this->l('Display widgets for sidebar or something');

    }

    function install() {

        if (parent::install() && $this->registerHook('displayHeader')){
            $res = $this->createTable();
            if ($res) {
                $this->installSamples();
            }
            return $res;
        }

        return false;

    }

    function uninstall() {

        if (parent::uninstall()) {
            $res = $this->deleteTable();
            return (bool)$res;
        }

        return false;

    }

    function hookdisplayHeader() {
        return $this->display(__FILE__, 'frontend.tpl');
    }

    private function createTable() {

        $sql = "DROP TABLE IF EXISTS " . _DB_PREFIX_ . "`awidget`;CREATE TABLE " . _DB_PREFIX_ . "`awidget` (
                `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                `key` varchar(255) NOT NULL,
                `value` longtext NOT NULL,
                `area` text NOT NULL COMMENT 'where to contains widget',
                `position` int(5) NOT NULL COMMENT 'order',
                `parent` bigint(20) NOT NULL COMMENT 'wrapper is parent or not',
                PRIMARY KEY (`id`),
                KEY `meta_key` (`key`)
              ) ENGINE=". _MYSQL_ENGINE_ ." DEFAULT CHARSET=utf8;";
        $res = (bool) Db::getInstance()->execute($sql);

        return $res;

    }

    private function installSamples() {
        $sql = "insert  into ". _DB_PREFIX_ . "`awidget`(`id`,`key`,`value`,`area`,`position`,`parent`) "
                . "values (1,'textwidget','a:2:{s:5:\"title\";s:14:\"The first text\";s:7:\"content\";s:232:\"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\";}','widget-area-sample',1,0);";
        DB::getInstance()->execute($sql);
    }

    private function deleteTable() {
        return Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'awidget`');
    }

}
P / S:我是prestashop的新手。我做同样的homelider模块,但没有成功。 widgetModel.php和frontend.tpl是空文件:)

1 个答案:

答案 0 :(得分:1)

此:

" . _DB_PREFIX_ . "`awidget`

应该是:

`"._DB_PREFIX_."awidget`

在所有地方。

  • 在一次执行中包含2个查询 - 拆分它们不是一个好习惯(如果可能的话)
  • 所有方法都应该返回true / false,包括installSamples,你必须在安装/卸载期间在if语句中包含它
  • 尽量避免&#34; IF EXISTS&#34; - 例如,安装之前的删除查询不是必需的,并且在卸载时你也不需要它,因为如果安装已通过,则表EXISTS。