0

enter code`Install schema.php

namespace Magik\Helloworld\Setup;

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

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

    // Get tutorial_simplenews table
    $tableName = $installer->getTable('magik_helloworld_slider');
    // Check if the table already exists
    if ($installer->getConnection()->isTableExists($tableName) != true) {
        // Create tutorial_simplenews table
        $table = $installer->getConnection()
            ->newTable($tableName)
            ->addColumn(
                'id',
                Table::TYPE_INTEGER,
                null,
                [
                    'identity' => true,
                    'unsigned' => true,
                    'nullable' => false,
                    'primary' => true
                ],
                'ID'
            )
            ->addColumn(
                'imagename',
                Table::TYPE_TEXT,
                null,
                ['nullable' => false, 'default' => ''],
                'name'
            )
            ->addColumn(
                'url',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable'=> true],
                'URL Link'
            )
            // ->addColumn(
            //     'image',
            //     \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            //     '255',
            //     ['nullable' =>false],
            //     'Banner Image'
            // )
            ->addColumn(
                'summary',
                Table::TYPE_TEXT,
                null,
                ['nullable' => false, 'default' => ''],
                'Summary'
            )
            ->addColumn(
                'description',
                Table::TYPE_TEXT,
                null,
                ['nullable' => false, 'default' => ''],
                'Description'
            )
            ->addColumn(
                'store_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                null,
                ['identity' =>false,'unsigned' =>true, 'nullable' =>false],
                'Store_Id'
            )
            // ->addColumn(
            //     'order',
            //     \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            //     null,
            //     ['nullable' =>true],
            //     'Sort Order'
            // )
            ->addColumn(
                'created_at',
                Table::TYPE_DATETIME,
                null,
                ['nullable' => false],
                'Created At'
            )
            ->addColumn(
                'status',
                Table::TYPE_SMALLINT,
                null,
                ['nullable' => false, 'default' => '0'],
                'Status'
            )
            ->addForeignKey(
            $installer->getFKName('magik_helloworld_slider','store_id','store','store_id'),'store_id',
            $installer->getTable('store'),
            'store_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_NO_ACTION)
            ->addIndex(
            $setup->getIdxName(
            $installer->getTable('magik_helloworld_slider'),
            ['imagename'],
            \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT
            ),
            ['imagename'],
            ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_FULLTEXT]
        )->setComment('magik_helloworld_slider')
            ->setOption('type', 'InnoDB')
            ->setOption('charset', 'utf8');
        $installer->getConnection()->createTable($table);
    }

    $installer->endSetup();
}

} ` hereblcok file[![][1]]2I have created admin grid form. After saving form image couldn't get save into the database and directory.Other records save successfully in the database.

0
if (isset($files['image'])) {
                if (isset($files['image']['name']) and (file_exists($files['image']['tmp_name']))) {
                    // @codingStandardsIgnoreEnd
                    $uploader = $this->mediaStorageFileUploader->create(['fileId' => $files['image']]);
                    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                    $uploader->setAllowRenameFiles(true);
                    $uploader->setFilesDispersion(true);
                    $path = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath(
                        'simple_image/'
                    );

                    $result = $uploader->save($path, $files['image']['name']);

                    $image = 'simple_image' . $result['file'];
                } else {
                    $value = $data['image']['value'];
                    if (isset($data['image']['delete'])) {
                        $imageDelete = $data['image']['delete'];
                        $image = (isset($imageDelete) && $imageDelete == 1) ? '' : $value;
                    } else {
                        $image = $value;
                    }
                }
            }

Create a folder in /pub/media/simple_image folder and check it with the above source code. Add new column-like and change the column name which you want.

->addColumn( 'image', Table::TYPE_TEXT, 255, [ 'nullable' => true, 'default' => null, ], 'Image' )

| improve this answer | |
  • okk ..I will try – shital Mar 11 at 11:14
  • You must need to create a folder in pub/media/sample_image. – Tushar Mar 11 at 11:15
  • This Module is the UI component? If UI component then we need to make changes for UI component. For UI component we need to add entries in the di.xml file. – Tushar Mar 11 at 11:17
  • I have created module using block method without ui component. – shital Mar 11 at 11:19
  • Okay, Is it possible to share your table install schema? – Tushar Mar 11 at 11:20

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.