分类
后端

Magento2 新建数据表

#新建
#/Setup/InstallSchema.php
<?php


namespace Maijindou\FormAgent\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**  * @codeCoverageIgnore */
class InstallSchema implements InstallSchemaInterface
{
    /**  * {@inheritdoc}  * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        /**  * Create table 'greeting_message'  */
        $setup->startSetup();
        $table =
            $setup->getConnection()->newTable(
                $setup->getTable('form_agent')
            )->addColumn(
                'agent_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'nullable' => false, 'primary' => true],
                'Agent ID'
            )->addColumn(
                'name',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Name'
            )->addColumn(
                'company',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Company'
            )->addColumn(
                'email',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Email'
            )->addColumn(
                'tel',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Tel'
            )->addColumn(
                'product',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                1000,
                ['nullable' => true],
                'In which market would you like to sell our products'
            )->addColumn(
                'clean',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                1000,
                ['nullable' => true],
                'Have you sold cleaning product before?'
            )->addColumn(
                'interested',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                1000,
                ['nullable' => true],
                'What kinds of our products are you interested in?'
            )->addColumn(
                'cooperation',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                1000,
                ['nullable' => true],
                'Other important information you want to offer for cooperation'
            )->addColumn(
                'created_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'create time'
            )->addColumn(
                'updated_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'update time'
            )
            ->setComment("To Be An Agent");
        $setup->getConnection()->createTable($table);
    }
}

设置索引(唯一索引,普通索引)

#在 addColumn  后面添加如下
->addIndex(
            $setup->getIdxName(
                $installer->getTable('fme_googlemapsstorelocator'),
                ['email'],
                \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
            ),
            ['email'],
            ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
        )