Dodaj kolumnę Schemat aktualizacji Magento 2


11

Chcę wstawić nowe pole dla tabeli bazy danych do mojego niestandardowego rozszerzenia przy użyciu schematu aktualizacji, postępując zgodnie z tym postem , ale dostałem komunikat o błędzie:

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

oto mój kod:

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

stworzyłeś tabelę lime_eleveniacategory?
— Rakesh Jesadiya 10.04.17

@RakeshJesadiya tak, tabela znajduje się w bazie danych
— Shell Suite

prosimy o udostępnienie pełnego pliku kodu
— Rakesh Jesadiya

@RakeshJesadiya sprawdź mój zaktualizowany kod
— Shell Suite

zaktualizowałem odpowiedź, proszę sprawdzić.
— Rakesh Jesadiya 10.04.17

Odpowiedzi:


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

Możesz również uzyskać więcej informacji tutaj, tabela bazy danych aktualizacji


cześć, zrobiłem to samo, ale kolumna nie została dodana do tabeli.
— Sarfaraj Sipai

Jak utworzyć nową tabelę za pomocą aktualizacji. mam już moduł
— jafar pinjar

cześć, @Rakesh, jak dodać nowe kolumny między wyjściowymi kolumnami?
— jafar pinjar

@Rakesh Jak mogę przekazać tam wersję? Mówię o 1.0.1
— Magecode

W moim module.xml setup_version = "2.0.0"
— Magecode

2

Jeszcze jedna rzecz do zrobienia tutaj. Zaktualizuj module.xmlwersję. I zaktualizuj konfigurację, wykonaj ponowne indeksowanie i usuń pamięć podręczną. To będzie działać.


1

Aby dodać wielokrotność kolumn

    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

Próbowałem tego

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


        }
}

LUB

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


        }
}

Uwaga: jeśli napotkasz jakikolwiek problem, przyczyną może być moduł, który już zainstalowałeś. Jak wiadomo, jeśli moduł jest już zainstalowany, to polecenie setup: upgrade nie instaluje schematu. Musisz zajrzeć do tabeli setup_module, usunąć moduł z tabeli i ponownie uruchomić komendę php bin / magento setup: upgrade.

Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.