New table by UpgradeSchema

Route: code/Vender_name/Module_name/Setup/UpgradeSchema.php

<?php
namespace Vender_name\Module_name\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.1') < 0) {
            if ($setup->getConnection()->isTableExists($setup->getTable('supplier_data')) != true) {
                $table = $setup->getConnection()
                    ->newTable($setup->getTable('supplier_data'))
                    ->addColumn(
                        'supplier_id' ,
                        Table::TYPE_INTEGER ,
                        null ,
                        [
                            'identity' => true ,
                            'unsigned' => true ,
                            'nullable' => false ,
                            'primary' => true
                        ] ,
                        'Supplier Id'
                    )->addColumn(
                        'supplier_name' ,
                        Table::TYPE_TEXT ,
                        255 ,
                        ['nullable' => false , 'default' => ''] ,
                        'Supplier Name'
                    )->addColumn(
                        'mobile' ,
                        Table::TYPE_TEXT ,
                        255 ,
                        ['nullable' => false , 'default' => ''] ,
                        'Supplier Mobile'
                    )->addColumn(
                        'address' ,
                        Table::TYPE_TEXT ,
                        255 ,
                        ['nullable' => false , 'default' => ''] ,
                        'Supplier Address'
                    )->addColumn(
                        'city' ,
                        Table::TYPE_TEXT ,
                        255 ,
                        ['nullable' => false , 'default' => ''] ,
                        'Supplier City'
                    )->addColumn(
                        'country' ,
                        Table::TYPE_TEXT ,
                        255 ,
                        ['nullable' => false , 'default' => ''] ,
                        'Supplier Country'
                    )->addColumn(
                        'link' ,
                        Table::TYPE_TEXT ,
                        255 ,
                        ['nullable' => false , 'default' => ''] ,
                        'Supplier Link'
                    )->addColumn(
                        'status' ,
                        Table::TYPE_SMALLINT,
                        null ,
                        ['nullable' => false , 'default' => 0] ,
                        'Status'
                    )->addColumn(
                        'created_at' ,
                        Table::TYPE_TIMESTAMP ,
                        null ,
                        ['nullable' => false , 'default' => ''] ,
                        'Created Date'
                    )->addColumn(
                        'updated_at' ,
                        Table::TYPE_TIMESTAMP ,
                        null ,
                        ['nullable' => false , 'default' => ''] ,
                        'Updated Date'
                    )
                    ->setComment('Supplier Table')
                    ->setOption('type', 'InnoDB')
                    ->setOption('charset', 'utf8');
                $setup->getConnection()->createTable($table);
            }
        }

        $setup->endSetup();
    }
}

执行 php bin/magento setup:upgrade
注:执行命令前检查module.xml文件中的版本是否更改

Create Model, Resource Model and Collection

1)Create Model

<?php
namespace Vender_name\Module_name\Model;

class Supplier extends \Magento\Framework\Model\AbstractModel
{
    /**
     * Initialize resource model
     */
    protected function _construct()
    {
        $this->_init('Vender_name\Module_name\Model\Resource\Supplier');
    }
}

2)Create Resource Model

<?php
namespace Vender_name\Module_name\Model\Resource;

class Supplier extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     * Initialize resource model
     */
    protected function _construct()
    {
        $this->_init('supplier_data', 'supplier_id');
    }
}

3)Create Collection

<?php
namespace Vender_name\Module_name\Model\Resource\Supplier;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'Vender_name\Module_name\Model\Supplier',
            'Vender_name\Module_name\Model\Resource\Supplier'
        );
    }
    
    protected function _beforeLoad()
    {
        parent::_beforeLoad();
        $this->setOrder('supplier_id', 'DESC');
        return $this;
    }

后续获取 supplier 表中的数据直接通过模型对象获取即可;
示例:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$supplier = $objectManager->get('Vender_name\Module_name\Model\Supplier')->load($supplierId);
$data = $supplier->getData();

针尖怼麦芒
0 声望0 粉丝

引用和评论

0 条评论