Magento在sales_flat_quote表中添加一列并添加数据

时间:2015-06-09 10:11:31

标签: mysql magento

我来自之前的环境,其中诸如修改查询和添加列之类的操作只是编写sql并执行它的问题。但是,现在我在Magento工作,我想做“Magento方式”。

场景:我们使用paypal express,在控制器重定向到paypal之前,我真的想在sales_flat_quote中添加一个字段(如果不存在),称为paypal_status - 并设置值= 1(我们称之为,发送给PayPal)。

返回时我想将其更新为2或3(返回和挂起的事务,或返回和捕获的事务)。

所以我需要知道如何做两件事:

  1. 有类似$db->addColumn('paypal_status')的内容,只有在不存在时才会添加,

  2. UPDATE sales_flat_quote SET paypal_status = 1 WHERE entity_id = {whatever}

  3. 这将在... Paypal_Express类。

2 个答案:

答案 0 :(得分:1)

打开数据库并触发此SQL:更改表sales_flat_quote添加paypal_status tinyint(1)NOT NULL DEFAULT 1;

或者,您可以在自定义模块的SQL文件(位于CompanyName \ MyModuleName \ sql \ companyname_modulename_setup)中编写以下内容。此文件只执行一次,这是第一次安装模块。那时你的自定义列将不在数据库中,因此它将创建一个。

$installer = $this;
$installer->startSetup();
$installer->run("ALTER TABLE `{$installer->getTable('sales/quote')}` ADD `paypal_status` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'My Custom Paypal Status';");
$installer->endSetup();

清除所有cahces。

保存数据:

$myValue = 2;
Mage::getSingleton("checkout/cart")->getQuote()->setPaypalStatus($myValue)->save();

Mage :: getSingleton(“checkout / cart”) - > getQuote()会为您提供当前报价。

答案 1 :(得分:0)

在CompanyName \ MyModuleName \ sql \ companyname_modulename_setup的sql文件中,复制以下代码以创建列。

$installer = $this;
$installer->startSetup();
$installer->getConnection()
->addColumn($installer->getTable('sales/quote'),
    'paypal_status',
    array(
        'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
        'nullable'  => true,
        'comment' => 'Paypal Status',
    )
);
$installer->endSetup();

注销并登录,并刷新magento缓存以将列添加到表中。

Express Checkout控制器位于app / code / core / Mage / Paypal / Controller / Express / Abstract.php中。如果你想在控制器重定向到paypal之前添加一个字段,你可以像这样修改_initCheckout()方法:

protected function _initCheckout()
$quote = $this->_getQuote();
    if (!$quote->hasItems() || $quote->getHasError()) {
        $this->getResponse()->setHeader('HTTP/1.1','403 Forbidden');
        Mage::throwException(Mage::helper('paypal')->__('Unable to initialize Express Checkout.'));
    }
 $quote->setPaymentStatus(1); // Here is your change
 $this->_checkout = Mage::getSingleton($this->_checkoutType, array(
        'config' => $this->_config,
        'quote'  => $quote,
    ));
 return $this->_checkout;
}
相关问题