Advertisement
bogdanbatsenko

Install Upgrade Scripts 1

Apr 9th, 2022 (edited)
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.20 KB | None | 0 0
  1. INSTALL DATA PHP
  2. <?php
  3.  
  4. namespace Vaimo\Brand\Setup;
  5.  
  6. use Magento\Eav\Setup\EavSetup;
  7. use Magento\Eav\Setup\EavSetupFactory;
  8. use Magento\Framework\Setup\InstallDataInterface;
  9. use Magento\Framework\Setup\ModuleContextInterface;
  10. use Magento\Framework\Setup\ModuleDataSetupInterface;
  11.  
  12. class InstallData implements InstallDataInterface
  13. {
  14.     private $eavSetupFactory;
  15.  
  16.     public function __construct(EavSetupFactory $eavSetupFactory)
  17.     {
  18.         $this->eavSetupFactory = $eavSetupFactory;
  19.     }
  20.  
  21.     public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  22.     {
  23.         $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
  24.         $eavSetup->addAttribute(
  25.             \Magento\Catalog\Model\Product::ENTITY,
  26.             'trainee_brand_attribute',
  27.             [
  28.                 'type' => 'text',
  29.                 'backend' => '',
  30.                 'frontend' => '',
  31.                 'label' => 'Trainee Brand Attribute',
  32.                 'input' => 'select',
  33.                 'class' => '',
  34.                 'source' => 'Vaimo\Brand\Model\Config\Source\Options',
  35.                 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
  36.                 'visible' => true,
  37.                 'required' => true,
  38.                 'user_defined' => false,
  39.                 'default' => '',
  40.                 'searchable' => false,
  41.                 'filterable' => false,
  42.                 'comparable' => false,
  43.                 'visible_on_front' => false,
  44.                 'used_in_product_listing' => true,
  45.                 'unique' => false,
  46.                 'apply_to' => ''
  47.             ]
  48.         );
  49.     }
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. UPGRADE DATA PHP
  57. <?php
  58. namespace Vaimo\Brand\Setup;
  59.  
  60. use Magento\Framework\Setup\UpgradeDataInterface;
  61. use Magento\Framework\Setup\ModuleDataSetupInterface;
  62. use Magento\Framework\Setup\ModuleContextInterface;
  63.  
  64. class UpgradeData implements UpgradeDataInterface
  65. {
  66.     protected $_postFactory;
  67.  
  68.     public function __construct(\Vaimo\Brand\Model\PostFactory $postFactory)
  69.     {
  70.         $this->_postFactory = $postFactory;
  71.     }
  72.  
  73.     public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  74.     {
  75.         if (version_compare($context->getVersion(), '1.0.0', '<')) {
  76.             $data = [
  77.                 'brand_id'         => "455867895467",
  78.                 'brand_title' => "Absolutely Awesome  Brand",
  79.                 'brand_origin'      => 'United Kingdom'
  80.             ];
  81.             $post = $this->_postFactory->create();
  82.             $post->addData($data)->save();
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. INSTALL SCHEMA PHP
  92. namespace Vaimo\Brand\Setup;
  93.  
  94. class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
  95. {
  96.  
  97.     public function install(\Magento\Framework\Setup\SchemaSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
  98.     {
  99.         $installer = $setup;
  100.         $installer->startSetup();
  101.         if (!$installer->tableExists('vaimo_brand')) {
  102.             $table = $installer->getConnection()->newTable(
  103.                 $installer->getTable('vaimo_brand')
  104.             )
  105.                 ->addColumn(
  106.                     'brand_id',
  107.                     \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  108.                     null,
  109.                     [
  110.                         'identity' => true,
  111.                         'nullable' => false,
  112.                         'primary'  => true,
  113.                         'unsigned' => true,
  114.                     ],
  115.                     'Brand Id'
  116.                 )
  117.                 ->addColumn(
  118.                     'brand_title',
  119.                     \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  120.                     255,
  121.                     ['nullable => false'],
  122.                     'Brand Title'
  123.                 )
  124.                 ->addColumn(
  125.                     'brand_сountry',
  126.                     \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  127.                     255,
  128.                     [],
  129.                     'Brand Country'
  130.                 )
  131.                 ->setComment('Trainee Brand Table');
  132.             $installer->getConnection()->createTable($table);
  133.         }
  134.         $installer->endSetup();
  135.     }
  136. }
  137.  
  138.  
  139.  
  140.  
  141. UPGRADE SCHEMA  PHP
  142. namespace Vaimo\Brand\Setup;
  143.  
  144. use Magento\Framework\Setup\UpgradeSchemaInterface;
  145. use Magento\Framework\Setup\SchemaSetupInterface;
  146. use Magento\Framework\Setup\ModuleContextInterface;
  147.  
  148. class UpgradeSchema implements UpgradeSchemaInterface
  149. {
  150.     public function upgrade( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
  151.         $installer = $setup;
  152.  
  153.         $installer->startSetup();
  154.  
  155.         if(version_compare($context->getVersion(), '1.0.0', '<')) {
  156.             $installer->getConnection()->addColumn(
  157.                 $installer->getTable( 'vaimo_brand' ),
  158.                 'test',
  159.                 [
  160.                     'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
  161.                     'nullable' => true,
  162.                     'length' => '12,4',
  163.                     'comment' => 'test',
  164.                     'after' => 'brand_title'
  165.                 ]
  166.             );
  167.         }
  168.  
  169.  
  170.  
  171.         $installer->endSetup();
  172.     }
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement