Tambahkan kolom Skema Upgrade Magento 2

11

Saya ingin menyisipkan bidang baru untuk tabel database di ekstensi kustom saya menggunakan skema peningkatan dengan mengikuti posting ini , tetapi saya mendapat kesalahan yang mengatakan:

  [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` 

ini kode saya:

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();
    }
}
Shell Suite
sumber
Sudahkah Anda membuat tabel lime_eleveniacategory?
Rakesh Jesadiya
@RakeshJesadiya ya tabel ada di database
Shell Suite
silakan bagikan file kode lengkap Anda
Rakesh Jesadiya
@RakeshJesadiya periksa kode saya yang diperbarui
Shell Suite
saya telah memperbarui jawaban, silakan periksa.
Rakesh Jesadiya

Jawaban:

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();
    }
}

Anda bisa mendapatkan detail lebih lanjut di sini juga, tabel Upgrade Database

Rakesh Jesadiya
sumber
halo, saya telah melakukan hal yang sama tetapi kolom tidak ditambahkan ke tabel.
Sarfaraj Sipai
Cara membuat tabel baru menggunakan upgradeschema. saya sudah punya modul
jafar pinjar
halo, @Rakesh, bagaimana cara menambahkan kolom baru di antara kolom yang keluar?
jafar pinjar
@Rakesh Bagaimana saya bisa melewati versi di sana? Saya katakan tentang 1.0.1
Magecode
Dalam module.xml saya setup_version = "2.0.0"
Magecode
2

Satu hal lagi yang harus dilakukan di sini. Perbarui module.xmlversi. Dan tingkatkan pengaturan, lakukan pengindeksan ulang dan hapus cache. Itu akan berhasil.

Apurba Bhowmik
sumber
1

Untuk menambahkan beberapa kolom

    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();
}
nacho
sumber
0

Saya mencoba ini

    <?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();


        }
}

ATAU

<?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();


        }
}

Catatan: Jika Anda menghadapi masalah apa pun, itu mungkin karena modul yang sudah Anda instal. Seperti yang Anda ketahui, jika modul sudah diinstal maka setup: perintah upgrade tidak menginstal skema. Anda perlu melihat ke dalam tabel setup_module Anda, menghapus modul Anda dari tabel dan menjalankan kembali setup bin / magento php: perintah upgrade.

Rohit Kaushik
sumber