添加列升级架构Magento 2


11

我想通过遵循这篇文章,在我的自定义扩展中使用升级模式为数据库表插入新字段,但出现错误提示:

  [Zend_Db_Statement_Exception]                                                
  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'Category Depth.l  
  ime_eleveniacategory' doesn't exist, query was: DESCRIBE `Category Depth`.`  
  lime_eleveniacategory` 

这是我的代码:

namespace Test\TestAgain\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), "1.0.0", "<")) {
        //Your upgrade script
        }
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
          $tableName = $setup->getTable('lime_eleveniacategory'); 
          if ($setup->getConnection()->isTableExists($tableName) == true) {
                $connection = $setup->getConnection();
                $connection->addColumn(
                    $tableName,
                    'category_depth',
                    ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,'nullable' => false, 'afters' => 'category_name'],
                    'Category Depth'
                );
            }
        }
        $setup->endSetup();
    }
}

您是否创建了lime_eleveniacategory表?
Rakesh Jesadiya

@RakeshJesadiya是的,数据库中有此表
Shell Suite

请分享您的完整代码文件
Rakesh Jesadiya'Apr

@RakeshJesadiya检查我的更新代码
Shell Suite

我已经更新了答案,请检查。
Rakesh Jesadiya

Answers:


33
namespace Test\TestAgain\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $installer = $setup;

        $installer->startSetup();
        if (version_compare($context->getVersion(), "1.0.0", "<")) {
        //Your upgrade script
        }
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
          $installer->getConnection()->addColumn(
                $installer->getTable('lime_eleveniacategory'),
                'category_depth',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    'length' => 10,
                    'nullable' => true,
                    'comment' => 'Category Depth'
                ]
            );
        }
        $installer->endSetup();
    }
}

您也可以在此处获取更多详细信息,升级数据库表


你好,我做了同样的事情,但列未添加到表中。
Sarfaraj Sipai '18

如何使用upgradeschema创建新表。我已经有模块了
贾法尔

你好,@ Rakesh,如何在现有列之间添加新列?
贾法尔·品哈尔(Jafar Pinjar),

@Rakesh如何在那里传递版本?我说的是1.0.1
Magecode,

在我的module.xml setup_version =“ 2.0.0”
Magecode中,


1

添加倍数列

    if (version_compare($context->getVersion(), '0.1.1', '<')) {
        $installer->getConnection()->addColumn(
            $installer->getTable('reply_newsletter_subscriber'),
            'field_1',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'nullable' => true,
                'comment' => 'Field_1'
            ]

        );
        $installer->getConnection()->addColumn(
            $installer->getTable('reply_newsletter_subscriber'),
            'field_2',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'length' => 255,
                'nullable' => true,
                'comment' => 'Field_2'
            ]
        );
    }
    $installer->endSetup();
}

0

我试过了

    <?php


namespace Test\TestAgain\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Framework\DB\Ddl\Table;
use Magento\Quote\Setup\QuoteSetup;
use Magento\Sales\Setup\SalesSetup;

class InstallData implements InstallDataInterface
{
    protected $quoteSetupFactory;
    protected $salesSetupFactory;

    public function __construct(
        QuoteSetupFactory $quoteSetupFactory,
        SalesSetupFactory $salesSetupFactory
    ) {
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
         $quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);
         $salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);

        $setup->startSetup();

        $setup->startSetup();
        $quoteInstaller->addAttribute('quote', 'custom_column', ['type' => Table::TYPE_TEXT]);
        $salesInstaller->addAttribute('order', 'custom_column', ['type' => Table::TYPE_TEXT]);
        $setup->endSetup();


        }
}

要么

<?php


namespace Test\TestAgain\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Framework\DB\Ddl\Table;
use Magento\Quote\Setup\QuoteSetup;
use Magento\Sales\Setup\SalesSetup;

class UpgradeSchema implements UpgradeSchemaInterface
{


    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {


         $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();
        $connection->addColumn($installer->getTable('quote'), 'custom_column', [
            'type'     => Table::TYPE_TEXT,
            'nullable' => true,
            'comment'  => 'Custom Column Name'
        ]);
        $connection->addColumn($installer->getTable('sales_order'), 'custom_column', [
            'type'     => Table::TYPE_TEXT,
            'nullable' => true,
            'comment'  => 'Custom Column Name'
        ]);

        $installer->endSetup();


        }
}

注意:如果遇到任何问题,可能是由于已安装的模块所致。如您所知,如果模块已经安装,那么setup:upgrade命令不会安装模式。您将需要查看setup_module表,从表中删除模块,然后重新运行php bin / magento setup:upgrade命令。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.