温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - Module lang not working in Prestashop 1.7.6
php prestashop prestashop-1.7 prestashop-modules

php - 模块lang在Prestashop 1.7.6中不起作用

发布于 2020-04-22 15:07:00

只是尝试使用翻译创建一个新的简单模块,但是保存过程无法正常工作。表“ ps_myoptions_lang”正在使用id_myoptions = 0每个lang_id中的字段进行更新,并且“ ps_myoptions”中未保存任何内容。

在此处输入图片说明

modules / myoptions / controllers / admin / AdminOptionsController.php

<?php


require_once(dirname(__FILE__) . './../../classes/Option.php');

class AdminOptionsController extends AdminController
{
    public function __construct(){
        parent::__construct();
        $this->bootstrap = true; // use Bootstrap CSS
        $this->table = 'myoptions'; // SQL table name, will be prefixed with _DB_PREFIX_
        $this->lang = true;
        $this->identifier = 'id_myoptions'; // SQL column to be used as primary key



        $this->className = 'Option'; // PHP class name
        $this->allow_export = true; // allow export in CSV, XLS..


        $this->_defaultOrderBy = 'a.name'; // the table alias is always `a`
        $this->_defaultOrderWay = 'DESC';
        $this->fields_list = [
            'id_myoptions' => ['title' => 'ID','class' => 'fixed-width-xs'],
            'name' => ['title' => 'Name'],
        ];

        $this->addRowAction('edit');
        $this->addRowAction('details');

        $this->fields_form = [
            'legend' => [
                'title' => 'Pasta',
                'icon' => 'icon-list-ul'
            ],
            'input' => [
                ['name'=>'name','type'=>'text', 'lang' => true, 'label'=>'Name','required'=>true],
            ],
            'submit' => [
                'title' => $this->trans('Save', [], 'Admin.Actions'),
            ]
        ];
    }
}

modules / myoptions / classes / Options.php

<?php


class Option extends ObjectModel
{
    public $id;
    public $name;

    public static $definition = [
        'table' => 'myoptions',
        'primary' => 'id_myoptions',
        'multilang' => true,
        'fields' => [
            'name' =>  ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isAnything', 'required'=>true],
        ],
    ];
}

modules / myoptions / myoptions.php

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

class Myoptions extends Module
{
    protected $config_form = false;


    public function __construct()
    {
        $this->name = 'myoptions';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'abc';
        $this->need_instance = 1;

        /**
         * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
         */
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('My Options');
        $this->description = $this->l('Add additional options');

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    }

    /**
     * Don't forget to create update methods if needed:
     * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
     */
    public function install()
    {
        Configuration::updateValue('MYOPTIONS_LIVE_MODE', false);

        include(dirname(__FILE__).'/sql/install.php');

        return parent::install() &&
            $this->registerHook('header') &&
            $this->registerHook('backOfficeHeader') &&
            $this->installTabs();
    }

    public function uninstall()
    {
        Configuration::deleteByName('MYOPTIONS_LIVE_MODE');

        include(dirname(__FILE__).'/sql/uninstall.php');

        return parent::uninstall();
    }

    public function enable($force_all = false)
    {
        return parent::enable($force_all)
            && $this->installTabs()
            ;
    }

    public function disable($force_all = false)
    {
        return parent::disable($force_all)
           && $this->uninstallTabs()
            ;
    }

    /**
     * Since PS 1.7.1
     * @var array
     */
    public $tabs = array(
        array(
            'name' => array(
                'en' => 'Options', // Default value should be first
                'fr' => 'Options',
            ),
            'class_name' => 'AdminOptions',
            'visible' => true,
            'parent_class_name' => 'AdminParentThemes',
        ),

    );

    public function installTabs()
    {
        $moduleName = $this->name;

        $this->addTab('AdminOptions', 'Options', $moduleName, 'AdminTools');




        return true;
    }
public function addTab($className, $tabName, $moduleName, $parentClassName)
    {
        $tab = new Tab();
        $tab->active = 1;
        $tab->class_name = $className;
        $tab->name = array();
        foreach (Language::getLanguages(true) as $lang) {
            $tab->name[$lang['id_lang']] = $tabName;
        }

        $tab->id_parent = (int) Tab::getIdFromClassName($parentClassName);

        $tab->module = $moduleName;

        $tab->add();

        return $tab;
    }
}

我做错什么了吗?

感谢您的回答。

Prestashop 1.7.6 PHP 7.2.25

查看更多

提问者
spiNOops
被浏览
20
spiNOops 2020-02-11 18:43

我想我发现了问题:

    $sqlCreate = "CREATE TABLE `" . _DB_PREFIX_ . "myoptions` (
                `id_myoptions` int(11) unsigned NOT NULL AUTO_INCREMENT,
                `name` varchar(255) DEFAULT NULL,
                PRIMARY KEY (`id_myoptions`)
                ) ENGINE=InnoDB DEFAULT CHARSET=latin1;";

$sqlCreateLang = "CREATE TABLE `" .  _DB_PREFIX_ . "myoptions_lang` (
              `id_myoptions` int(11) unsigned NOT NULL AUTO_INCREMENT,
              `id_lang` int(11) NOT NULL,
              `name` varchar(255) DEFAULT NULL,
              PRIMARY KEY (`id_myoptions`,`id_lang`)
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1;";

Db::getInstance()->execute($sqlCreate) && Db::getInstance()->execute($sqlCreateLang);

name在两个表中都有相同的字段然后,我仅在Lang Table中使用它,现在似乎可以使用了。