ankithg03
9/23/2019 - 5:50 AM

SetupSchema

Setup

  InstallData.php

  InstallSchema.php

  UpgradeData.php

<?php

namespace Codilar\ProductVendor\Setup;

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

class InstallData implements InstallDataInterface
{

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $data = [
            [
                'vendor_name'=>'Bashid',
                'vendor_email'=>'bashizee@gmail.com',
                'vendor_phone_number'=>'+918746850422',
                'vendor_address'=>'Kasargud',
            ],
            [
                'vendor_name'=>'Rajath',
                'vendor_email'=>'rajath@gmail.com',
                'vendor_phone_number'=>'+918746850422',
                'vendor_address'=>'Mangalore',
            ],
            [
                'vendor_name'=>'Karthik',
                'vendor_email'=>'karthik@gmail.com',
                'vendor_phone_number'=>'+918746850422',
                'vendor_address'=>'Dendakal',
            ],
            [
                'vendor_name'=>'Pradhyumna',
                'vendor_email'=>'pradyumna@gmail.com',
                'vendor_phone_number'=>'+918746850422',
                'vendor_address'=>'Manglore',
            ],
            [
                'vendor_name'=>'Hrashikesh',
                'vendor_email'=>'hrashikesh@gmail.com',
                'vendor_phone_number'=>'+918746850422',
                'vendor_address'=>'Sirsi',
            ]
        ];
        foreach ($data as $row) {
            $setup->getConnection()
                ->insertForce($setup->getTable('codilar_vendor'), $row);
        }
        // TODO: Implement install() method.
    }
}
<?php

namespace Codilar\ProductVendor\Setup;

use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{

    /**
     * Installs DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $installer = $setup;
        $table = $installer->getConnection()->newTable(
            $installer->getTable('codilar_vendor')
        )
        ->addColumn(
          'vendor_id',
          Table::TYPE_INTEGER,
          null,
          [
              'nullable' => false,
              'identity'  => true,
              'primary' => true,
          ],
          'Vendor Unique Id'
        )
        ->addColumn(
            'vendor_name',
            Table::TYPE_TEXT,
            200,
            [

            ],
            'Vendor Name'
        )
        ->addColumn(
            'vendor_email',
            Table::TYPE_TEXT,
            200,
            [

            ],
            'Vendor Email'
        )
        ->addColumn(
            'vendor_phone_number',
            Table::TYPE_TEXT,
            200,
            [

            ],
            'Vendor Phone Number'
        )
        ->addColumn(
            'vendor_address',
            Table::TYPE_TEXT,
            200,
            [

            ],
            'Vendor Address'
        )
        ->addColumn(
            'product_sku',
            Table::TYPE_BLOB,
            1000,
            [

            ],
            'Product SKU'
        )
        ->setComment(
            'Vendor\'s Data Table'
        );
        $setup->getConnection()
            ->createTable($table);
        $setup->getConnection()
            ->addIndex(
                $installer->getConnection()->getTableName('codilar_vendor'),
                $setup->getIdxName(
                   $installer->getConnection()->getTableName('codilar_vendor'),
                   [
                       'vendor_name',
                       'vendor_email',
                       'vendor_phone_number',
                       'vendor_address'
                   ],
                   AdapterInterface::INDEX_TYPE_UNIQUE
               ),
                [
                    'vendor_name',
                    'vendor_email',
                    'vendor_phone_number',
                    'vendor_address'
                ],
                AdapterInterface::INDEX_TYPE_UNIQUE
            );
        $setup->getConnection()
            ->addIndex(
                $installer->getConnection()->getTableName('codilar_vendor'),
                $setup->getIdxName(
                    $installer->getConnection()->getTableName('codilar_vendor'),
                    [
                        'vendor_email'
                    ],
                    AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                [
                    'vendor_email'
                ],
                AdapterInterface::INDEX_TYPE_UNIQUE
            );

        $setup->endSetup();
        // TODO: Implement install() method.
    }
}
<?php

namespace Codilar\ProductVendor\Setup;

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

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @var Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $setup;
    /**
     * @var \Magento\Eav\Model\AttributeManagement
     */
    private $attributeManagement;

    public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory, \Magento\Eav\Model\AttributeManagement $attributeManagement)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->attributeManagement = $attributeManagement;
    }

    /**
     * Upgrades data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $attributes = [
                'vendor_id' => [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Vendor',
                'input' => 'select',
                'class' => '',
                'source' => 'Codilar\ProductVendor\Model\Config\Source\Options',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => true,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        ];
        // TODO: Implement upgrade() method.
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        foreach ($attributes as $attribute_code => $attributeOptions) {
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                $attribute_code,
                $attributeOptions
            );
        }
//        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
//        $eavSetup->removeAttribute(
//            \Magento\Catalog\Model\Product::ENTITY,
//            'vendor_attribute');
    }

}