【问题标题】:Magento - How to add upgrade SQL lines to extensionMagento - 如何将升级 SQL 行添加到扩展
【发布时间】:2015-03-14 20:40:15
【问题描述】:

我正在开发自定义 Magento 扩展程序。我正在开发 Magento 1.9.0.1 版本的扩展。

所以我有一个简单的问题。

我创建了一个扩展,最近我对其进行了一些升级。 扩展的第一个版本编号:1.0.0

让我给你看一下扩展的配置文件:

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_SmsNotification>
      <version>1.0.0</version>
    </VivasIndustries_SmsNotification>
  </modules>
  <global>
    <models>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Model</class>
            <resourceModel>vivasindustries_smsnotification_resource</resourceModel>
        </smsnotification>
        <vivasindustries_smsnotification_resource>
        <class>VivasIndustries_SmsNotification_Model_Resource</class>
        <entities>
            <smsnotification>
            <table>VivasIndustries_SmsNotification</table>
            </smsnotification>
            <smsnotificationhistory>
            <table>VivasIndustries_SmsHistory</table>
            </smsnotificationhistory>
        </entities>
        </vivasindustries_smsnotification_resource>
    </models>
    <resources>
        <smsnotification_setup>
            <setup>
                <module>VivasIndustries_SmsNotification</module>
            </setup>
            <connection>
                 <use>core_setup</use>
             </connection>
        </smsnotification_setup>
        <smsnotification_read>
            <connection>
                <use>core_read</use>
            </connection>
        </smsnotification_read>
        <smsnotification_write>
            <connection>
                <use>core_write</use>
            </connection>
        </smsnotification_write>
    </resources>    
    <events>
    <checkout_type_onepage_save_order_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_type_onepage_save_order_after_smsprice_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>smsnotification/newordertotalobserver</class> <!-- observers class alias -->
            <method>saveSmspriceTotal</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_type_onepage_save_order_after_smsprice_handler>
        </observers>
      </checkout_type_onepage_save_order_after>  
    <checkout_type_multishipping_create_orders_single> <!-- identifier of the event we want to catch -->
        <observers>     
          <checkout_type_multishipping_create_orders_single_smsprice_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>smsnotification/newordertotalobserver</class> <!-- observers class alias -->
            <method>saveSmspriceTotalForMultishipping</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_type_multishipping_create_orders_single_smsprice_handler>      
        </observers>
      </checkout_type_multishipping_create_orders_single>     
        <sales_order_save_after>
            <observers>
                <vivasindustries_smsnotification>
                    <class>smsnotification/observer</class>
                    <method>orderSaved</method>
                </vivasindustries_smsnotification>
            </observers>
        </sales_order_save_after>
    </events>
     <sales>
        <quote>
            <totals>                
                <smsprice_total>
                    <class>smsnotification/quote_address_total_smsprice</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </smsprice_total> 
            </totals>
        </quote>
            <order_invoice>
                <totals>                
                <smsprice_total>
                    <class>smsnotification/order_invoice_total_smsprice</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </smsprice_total> 
                </totals>
            </order_invoice>
            <order_creditmemo>
                <totals>                
                <smsprice_total>
                    <class>percentpayment/order_creditmemo_total_smsprice</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </smsprice_total> 
                </totals>
            </order_creditmemo>
    </sales>    
    <helpers>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Helper</class>
        </smsnotification>
    </helpers>
    <blocks>
        <smsnotification>
             <class>VivasIndustries_SmsNotification_Block</class>
        </smsnotification>
    </blocks>
  </global>
  <adminhtml>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <vivas>
                                        <title>Vivas - All</title>
                                    </vivas>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
    <layout>
        <updates>
            <smsnotification>
                <file>smsnotification.xml</file>
            </smsnotification>
        </updates>
    </layout>   
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <VivasIndustries_SmsNotification before="Mage_Adminhtml">VivasIndustries_SmsNotification_Adminhtml</VivasIndustries_SmsNotification>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <default>
    <vivas>
        <smspricegroup>                
            <smsprice_name>SMS Notification</smsprice_name>
            <smsprice_fee>0.5</smsprice_fee>            
        </smspricegroup>      
    </vivas>
    </default>
</config>  

并且位于 /app/code/community/VivasIndustries/SmsNotification/sql/smsnotification_setup/mysql4-install-1.0.0.php 的 sql 安装程序文件包含:

<?php
$installer=$this;
$installer->startSetup();

$installer->run("
CREATE TABLE IF NOT EXISTS `VivasIndustries_SmsNotification` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `state` varchar(500) NOT NULL,
  `smstext` varchar(500) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `VivasIndustries_SmsHistory` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `receiver` varchar(500) NOT NULL,
  `phone` varchar(500) NOT NULL,
  `email` varchar(500) NOT NULL,
  `smstext` text NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

    ");
$installer->endSetup();
?>

但在升级后我必须将这个想法添加到 MySQL:

$installer->addAttribute("quote_address", "smsprice_total", array("type"=>"varchar"));
$installer->addAttribute("order", "smsprice_total", array("type"=>"varchar"));

那么我怎样才能创建一个升级文件,这个扩展又如何知道它有一个升级呢?

我什至不确定我的问题是否正确。

附: 我将导出 Magento 的整个 MySQL 数据库以搜索匹配项:smsprice_total,这样我就可以确定它们已创建。

谢谢!

【问题讨论】:

    标签: sql xml magento upgrade


    【解决方案1】:

    升级通常在生产服务器上运行。如果您正在开发中,只需将它们粘贴到您的原始安装程序文件中,只要您删除他们创建的表并从 core_resource 中删除相关条目,它们就会运行。

    在你的情况下,删除:VivasIndustries_SmsNotificationVivasIndustries_SmsHistory

    然后在core_resource中找到smsnotification_setup的入口并删除该行。

    如果您真的想添加升级脚本,只需执行以下操作:

    将您的模块版本更改为1.1.02.0.01.0.1 - 基本上任何高于原始版本的版本,以便它检测版本差异并运行您的升级脚本。

    <version>2.0.0</version>
    

    创建与您所做的版本更改匹配的以下文件:

    mysql4-upgrade-1.0.0-1.1.0.php
    

    mysql4-upgrade-1.0.0-2.0.0.php

    mysql4-upgrade-1.0.0-1.0.1.php

    刷新缓存并执行单个页面加载。

    如果在core_resource 表中更新了smsnotification_setup 的编号,您将知道扩展安装成功

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 2014-07-25
      • 2012-12-02
      • 2019-08-14
      • 2015-12-13
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多