1

I created an admin module for that I need Store View multiselect option. I created upgrade schema and checked it but still selected language is not selected after saving the page.In database table I am getting the column value as 0 (which means All Store Views).enter image description here

My code is like this in upgrade schema.

if (version_compare($context->getVersion(), '1.0.2') < 0) {

    // Get module table
    $tableName = $setup->getTable('adminsharan_adminsample');

    // Check if the table already exists
    if ($setup->getConnection()->isTableExists($tableName) == true) {
        // Declare data
        $columns = [
            'store_id' => [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                'nullable' => false,
                'primary' => false,
                'comment' => 'store id',
            ],
        ];
        $connection = $setup->getConnection();
        foreach ($columns as $name => $definition) {
            $connection->addColumn($tableName, $name, $definition);
        }

    }
}
  • Refer magento\vendor\magento\module-search\Setup\UpgradeSchema.php – Ankit Shah Dec 20 '16 at 10:10
3

Can you please try this:

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

        $table = $setup->getConnection()->newTable(
            $setup->getTable('inchoo_custom')
        )->addColumn(
            'custom_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Custom Id'
        )->addColumn(
            'name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Custom Name'
        )->setComment(
            'Custom Table'
        );
        $setup->getConnection()->createTable($table);

        $setup->endSetup();
}
2

For multi store select you should not create a column in your table.
Because you are making a many to many relation between your entity and the store view entity you should have an extra table with the columns your_entity_id and store_id with foreign keys on both columns.
One to your main table and one to the store views table.
This way, if you delete your entity or a store view everything will cascade nicely and you won't end up with zombie data in your db.
The best core example you can use for this is the CMS module. Pick the pages or the static blocks as example.
TO create the table you need to do this in your upgrade schema

$installer = $setup;

if (version_compare($context->getVersion(), '1.0.2') < 0) {
     $table = $installer->getConnection()->newTable(
            $installer->getTable('adminsharan_adminsample_store')
        )->addColumn(
            'adminsample_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['nullable' => false, 'primary' => true],
            'Sample ID'
        )->addColumn(
            'store_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true, 'nullable' => false, 'primary' => true],
            'Store ID'
        )->addIndex(
            $installer->getIdxName('adminsharan_adminsample_store', ['store_id']),
            ['store_id']
        )->addForeignKey(
            $installer->getFkName('adminsharan_adminsample_store', 'adminsample_id', 'adminsharan_adminsample', 'adminsample_id'),
            'adminsample_id',
            $installer->getTable('adminsharan_adminsample'),
            'adminsample_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )->addForeignKey(
            $installer->getFkName('adminsharan_adminsample_store', 'store_id', 'store', 'store_id'),
            'store_id',
            $installer->getTable('store'),
            'store_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )->setComment(
            'Sample To Store Linkage Table'
        );
        $installer->getConnection()->createTable($table);
}

And in order to insert and read data from that table once your entity is loaded take a look at the cms block resource model and check the methods that have something related to _store tables and add them yo your entity resource model. Like _getLoadSelect or lookupStoreIds. There might be others also.

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.