Skripte in Magento 2 einrichten

Setup Scripts In Magento 2

Erstellen Sie ein neues Modul mit CustomScripts_SetupScripts

Schritt 1: Erstellen Sie die Datei module.xml.

app/code/CustomScripts/SetupScripts/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="CustomScripts_SetupScripts" setup_version="1.0.0" />
</config>

Schritt 2: Erstellen Sie die Datei registration.php.

app/code/CustomScripts/SetupScripts/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
	\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'CustomScripts_SetupScripts',
	__DIR__
);

Schritt 3: Erstellen Sie die Datei InstallSchema.php.

app/code/CustomScripts/SetupScripts/Setup/InstallSchema.php

<?php

namespace CustomScripts\SetupScripts\Setup;

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

class InstallSchema implements InstallSchemaInterface
{

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        $table = $installer->getConnection()
            ->newTable($installer->getTable('inviqa_example'))
            ->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => 
false, 'primary' => true],
                'Entity ID'
            )
            ->addColumn(
                'name',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => false],
                'Name'
            )
            ->setComment('Inviqa Example');
        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }
}

Schritt 4: Erstellen Sie die Datei InstallData.php.

app/code/CustomScripts/SetupScripts/InstallData.php

<?php

namespace CustomScripts\SetupScripts\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->getConnection()->query("INSERT INTO inviqa_example SET name = 'Test 1'");
    }
}

Schritt 5: Erstellen Sie die Datei UpgradeSchema.php.

app/code/CustomScripts/SetupScripts/Setup/UpgradeSchema.php

<?php

namespace CustomScripts\SetupScripts\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.1.0', '<=')) {

            $setup->getConnection()->addColumn(
                $setup->getTable('inviqa_example'),
                'email',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'nullable' => true,
                    'comment' => 'Email'
                ]
            );

        }

        $setup->endSetup();
    }
}

Schritt 6: Erstellen Sie die Datei UpgradeData.php.

app/code/CustomScripts/SetupScripts/Setup/UpgradeData.php

<?php

namespace CustomScripts\SetupScripts\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements UpgradeDataInterface
{
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.1.0', '<=')) {
            $setup->getConnection()->query("INSERT INTO inviqa_example SET name = 'Test 2', email = 'test@inviqa.com'");
        }

        $setup->endSetup();
    }
}

Schritt 7: Erstellen Sie die Datei Recurring.php.

app/code/CustomScripts/SetupScripts/Setup/Recurring.php

<?php

namespace CustomScripts\SetupScripts\Setup;

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

class Recurring implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $setup->getConnection()->query("INSERT INTO inviqa_example SET name = 'Test 3', email = 'test@inviqa.com'");

        $setup->endSetup();
    }
}

Schritt 8: Erstellen Sie die Datei Uninstall.php.

app/code/CustomScripts/SetupScripts/Setup/Uninstall.php

<?php

namespace CustomScripts\SetupScripts\Setup;

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

class Recurring implements UninstallSchemaInterface
{
    public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $setup->getConnection()->query("DROP table inviqa_example");

        $setup->endSetup();
    }
}

Bitte kontaktieren Sie uns unter manish@bay20.com oder rufen Sie uns unter +91-8800519180 an, wenn Sie Unterstützung für Magento 2 benötigen. Sie können auch die Magento2-Entwicklungsseite besuchen, um die von uns angebotenen Dienstleistungen zu überprüfen.