2

in magento 1 code is

INSERT INTO {$this->getTable('table_name')} (`color_id`, `color_name`, `color_code`) VALUES
(1, 'Green', '#A2C44C'),
(2, 'White', '#FFFFFF'),
(3, 'Blue Dusk', '#304050 '),
(4, 'Black', '#28272c'),
(5, 'Azalea', '#e975a4'),
(6, 'Cardinal Red', '#891c2f'),
(7, 'Carolina Blue', '#6e95ce'),
(8, 'Cherry Red', '#b3042b'),
(9, 'Chestnut', '#7d5b52'),
(10, 'Daisy', '#fed348'),
(11, 'Dark Chocolate', '#3c3230'),
(12, 'Forest', '#333a32'),
(13, 'Gold', '#feac1c'),
(14, 'Heliconia', '#d1316f'),
(15, 'Honey', '#edb963'),
(16, 'Ice Grey', '#c2bebf'),
(17, 'Indigo Blue', '#4a5d7b'),
(18, 'Iris', '#497bba'),

how i set this code in the magento2 in installData.php

2

In your module folder, create a Setup folder and InstallData.php file

<?php

namespace Vendor\Module\Setup;

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

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /**
         * Prepare database for install
         */
        $setup->startSetup();

        $columns = [];
        $columns = ['color_id', 'color_name', 'color_code'];
        $data = [];
        $data[] = ['1', 'Green', '#A2C44C'];

        $setup->getConnection()
                ->insertArray($setup->getTable('your_table_name'), $columns, $data);

        /**
         * Prepare database after install
         */
        $setup->endSetup();
    }
}

PS: prior to this you need a InstallSchema.php in order to create your custom table

  • @Rahulocodewire you welcome! – Jonathan Ribas Feb 13 '17 at 7:52
  • can you tell me please how i insert one or more colors in this table when i upgrade get error my code is this $data[] =[('1', 'Green', '#A2C44C'), ['2', 'White', '#FFFFFF']; how i update two or more colors – Rahul Katoch Feb 13 '17 at 8:15
  • Try this: $data =[['1', 'Green', '#A2C44C'], ['2', 'White', '#FFFFFF']]; – Jonathan Ribas Feb 13 '17 at 8:27
  • this is not work – Rahul Katoch Feb 13 '17 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.