Membuat Module Magento 2

Pada catatan kali ini saya mencatat kegiatan saya dalam belajar membuat Module untuk Magento 2. sebelumnya saya sudah membuat catatan simple mengenai hal ini.  Catatan  ini sendiri adalah catatan belajar dari sini.

Source code lengkap disini.

Folder dan Files

Isi dari composer.json

Isi dari module.xml


Isi dari registration.php



Sampai disini saya telah memiliki Module Kosong yang akan saya gunakan untuk belajar.

Enable Module


Bisa juga dilihat disini untuk list modules apa saja yang sudah aktif atau tidak aktif.


Buat Tables Yang Diperlukan

FieldTypeNullKeyDefault
post_idint(10) unsignedNOPRINULL
titletextNONULL
contenttextNONULL
created_attimestampNOCURRENT_TIMESTAMP

Buat Files Yang Diperlukan

InstallSchema.php

<?php

namespace Toptal\Blog\Setup;

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

/**
* Class InstallSchema
*
* @package Toptal\Blog\Setup
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* Install Blog Posts table
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

$tableName = $setup->getTable('toptal_blog_post');

if ($setup->getConnection()->isTableExists($tableName) != true) {
$table = $setup->getConnection()
->newTable($tableName)
->addColumn(
'post_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'ID'
)
->addColumn(
'title',
Table::TYPE_TEXT,
null,
['nullable' => false],
'Title'
)
->addColumn(
'content',
Table::TYPE_TEXT,
null,
['nullable' => false],
'Content'
)
->addColumn(
'created_at',
Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
'Created At'
)
->setComment('Toptal Blog - Posts');
$setup->getConnection()->createTable($table);
}

$setup->endSetup();
}
}

UpgradeData.php

<?php

namespace Toptal\Blog\Setup;

use \Magento\Framework\Setup\UpgradeDataInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;

/**
* Class UpgradeData
*
* @package Toptal\Blog\Setup
*/
class UpgradeData implements UpgradeDataInterface
{

/**
* Creates sample blog posts
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if ($context->getVersion()
&& version_compare($context->getVersion(), '0.1.1') < 0
) {
$tableName = $setup->getTable('toptal_blog_post');

$data = [
[
'title' => 'Post 1 Title',
'content' => 'Content of the first post.',
],
[
'title' => 'Post 2 Title',
'content' => 'Content of the second post.',
],
];

$setup
->getConnection()
->insertMultiple($tableName, $data);
}

$setup->endSetup();
}
}

Upgrade Untuk Membuat Table

Table toptal_blog_post

Lakukan Upgrade Ke 0.1.1

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Toptal_Blog" setup_version="0.1.1">
<sequence>
<module name="Magento_Directory" />
<module name="Magento_Config" />
</sequence>
</module>
</config>

Upgrade Untuk Mengisi Table



Setup Module Sebelum Upgrade


Setup Module Setelah Upgrade


Membuat Model Post


Lorem Ipsum dsfsd

Comments

Popular posts from this blog

Numpang Kerja Remote dari Bandung Creative Hub

Numpang Kerja Remote dari Bandung Digital Valley

Cara Decompile berkas Dex dan Apk Android