cancel
Showing results for 
Search instead for 
Did you mean: 

Add store_id to custom module table Magento 2.2.2

Add store_id to custom module table Magento 2.2.2

I created a custom module where I can upload images to my product's description. I realized my products require different images for each of my store view (US, UK). My goal is to add different sets of images for each store view. My logic is to add store_id from the 'store' table into my custom module's data schema. I am a bit lost on how to go about doing this.

 

Here's my InstallSchema:

 

private function createFlipbookTable(SchemaSetupInterface $installer)
    {
        $fkName = $installer->getFkName(
            'flipbook',
            'product_id',
            'catalog_product_entity',
            'entity_id'
        );
        $table = $installer->getConnection()
            ->newTable($installer->getTable('flipbook'))
            ->addColumn(
                'entity_id',
                Table::TYPE_SMALLINT,
                null,
                ['identity' => true, 'nullable' => false, 'primary' => true],
                'Entity Id'
            )
            ->addColumn(
                'product_id',
                Table::TYPE_INTEGER,
                10,
                ['nullable' => false, 'unsigned' => true],
                'Product Id'
            )
            ->addColumn(
                'title',
                Table::TYPE_TEXT,
                255,
                ['nullable' => false],
                'Description Title'
            )
            ->addColumn(
                'description',
                Table::TYPE_TEXT,
                '2M',
                ['nullable' => false],
                'Description'
            )->addColumn(
                'image',
                Table::TYPE_TEXT,
                '400',
                ['nullable' => false],
                'Image'
            )
            ->addColumn(
                'position',
                Table::TYPE_SMALLINT,
                null,
                ['nullable' => false, 'default' => '0'],
                'Position'
            )
            ->addForeignKey(
                $fkName,
                'product_id',
                $installer->getTable('catalog_product_entity'),
                'entity_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Product Custom Description');

        $installer->getConnection()->createTable($table);
    }

My UpgradeSchema:

 

private function addProductEntityForeignKey(SchemaSetupInterface $setup)
    {
        $fkName = $setup->getFkName(
            'flipbook',
            'product_id',
            'catalog_product_entity',
            'entity_id'
        );
        $tableName = $setup->getTable('flipbook');
        $connection = $setup->getConnection();
        $connection->changeColumn(
            $tableName,
            'product_id',
            'product_id',
            [
                'type' => Table::TYPE_INTEGER,
                'nullable' => false,
                'unsigned' => true,
                'length' => 10
            ]
        );
        $connection->addForeignKey(
            $fkName,
            $tableName,
            'product_id',
            $setup->getTable('catalog_product_entity'),
            'entity_id',
            Table::ACTION_CASCADE
        );
    }

Let me know if you need more information! Thanks 

3 REPLIES 3

Re: Add store_id to custom module table Magento 2.2.2

Hi @tim_muangkeo,

 

Maybe you could take a look to Magento_Cms module and see how, for example, cms blocks are porcessed.

Re: Add store_id to custom module table Magento 2.2.2

I took a look at the Cms modules setup and schemas. I got everything up and running through the SQL tables. Thanks for the hint!

 

I am having trouble giving the option to display on the admin side just like how the Cms block looks like. I created a module to add additional images along with a title and description to each product.Screen Shot 2018-05-21 at 3.50.30 PM.png Each one of my store views needs to have different sets of images. Much like this:

Screen Shot 2018-05-21 at 3.55.00 PM.pngHow do I call the Store View (image above) and attach it to my custom component? If there is another clever way to approach this problem, I'm open to ideas.

 

The end goal is to have different images with different store views.

Thanks for reading. Anything helps!

Re: Add store_id to custom module table Magento 2.2.2

Hi @tim_muangkeo,

 

Did you checked the vendor/magento/module-cms/view/adminhtml/ui_component/cms_block_form.xml file?

 

There you'll find this section:

 

        <field name="storeviews" formElement="multiselect">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">block</item>
                    <item name="default" xsi:type="number">0</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
                <dataType>int</dataType>
                <label translate="true">Store View</label>
                <dataScope>store_id</dataScope>
            </settings>
            <formElements>
                <multiselect>
                    <settings>
                        <options class="Magento\Cms\Ui\Component\Listing\Column\Cms\Options"/>
                    </settings>
                </multiselect>
            </formElements>
        </field>