Part 3: Setup classes & databases Skip to end of metadata

In the last part of the series we cover creating our model & resource model so that we can interact with the database. However, we haven’t yet created our table! This part of the series will soon fix that!

I separated this into it’s own post as I felt it needed it’s own post!

In Magento 1.x you’ll know that to make changes to the database we would create setup/upgrade scripts to handle this. Well, that hasn’t changed, it’s just now we take an OOP approach.

Now, I’d like the mention something I noticed before we get started. As it stands with the beta version of Magento 2, I don’t personally understand how our setup classes link to speicifc versions of our modules. You’ll notice this too as we go forward, you may also ask yourself how you can control which order these can be run in. As it stands, I have no idea and hope to find out!

Let’s dive in!

Remember in etc/module.xml we defined the setup_version? Well, this is what is used to tell Magento 2 which version our module is currently at, and will decide if upgrade or setup scripts need to be run.

When you first install your module and run bin/magento setup:upgrade our Setup script will be ran, and a record inserted into a table called setup_module, which is the same as the core_resource table from Magento 1.x.

So let’s create our Setup class to install our database!


Create a file called: Setup/InstallSchema.php

namespace Ecommage\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 implements InstallSchemaInterface
     * Installs DB schema for a module
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        $installer = $setup;
        $table = $installer->getConnection()
                ['identity' => true, 'nullable' => false, 'primary' => true],
                'Post ID'
            ->addColumn('url_key', Table::TYPE_TEXT, 100, ['nullable' => true, 'default' => null])
            ->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Blog Title')
            ->addColumn('content', Table::TYPE_TEXT, '2M', [], 'Blog Content')
            ->addColumn('is_active', Table::TYPE_SMALLINT, null, ['nullable' => false, 'default' => '1'], 'Is Post Active?')
            ->addColumn('creation_time', Table::TYPE_DATETIME, null, ['nullable' => false], 'Creation Time')
            ->addColumn('update_time', Table::TYPE_DATETIME, null, ['nullable' => false], 'Update Time')
            ->addIndex($installer->getIdxName('blog_post', ['url_key']), ['url_key'])
            ->setComment('Ecommage Blog Posts');


The name of this class can be anything you want. You just need to implement the interfaceMagento\Framework\Setup\InstallSchemaInterface. What do you do if you want to create an upgrade script? Well, you’d implement Magento\Framework\Setup\UpgradeSchemaInterface.

If you need to check the version of your module between upgrades, you will now need to manage that yourself. You can find out more from my question on handling upgrade scripts in magento 2

Now we can head to the CLI and run the bin/magento command to install our database table!

bin/magento setup:upgrade


When you run this you should see something along the lines of:

$ bin/magento setup:db-schema:upgrade
Schema creation/updates:
Module 'Ecommage_Blog':
Installing schema..
Module 'Magento_Store':


That shows it has picked up your Schema Setup and ran it successfully! 

Note: If you run into any issues, it will be because you have already installed the module from the first part of my series. You will need to go into the setup_module table and delete your module from the table. Then re-run the bin/magento setup:upgrade command.

So far, we have created Model, Resource Model, Classes and Database that allow us define objects of the extension. It’s ready to build the features for the extension.

In the next article, we will start creating Controllers, Blocks and Template.


There is no comment on this post. Be the first one.

Leave a comment