Magento-2-新增表及Create-Model

39次阅读

共计 3137 个字符,预计需要花费 8 分钟才能阅读完成。

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