I want to create a new module in which I would like to have data of a specific database table. Is it possible ? If yes, please guide !


Create your custom module as per your requirements, then in the model of your module create the following method.


public function getYourRequiredData()

      $select =$this->_getResource()->getConnection()->select()->from($this->_getResource()->getTable('blog_post'))->where(1);
      $data = $this->_getResource()->getConnection()->fetchAll($select);
  • custom_id ? If I want to get recent blogs post from blog_post table? what I should do then ? – Learner Mar 19 '18 at 12:30
  • remove the where clause and replace your_custom_table_name with blog_post table – Verdu Mar 19 '18 at 12:33
  • custom_id is your primary key in your table it could be with any name , I include the custom_id if you want to get data specific to some particular Id . If you want to fetch all the records , then you don't need that custom_id – Verdu Mar 19 '18 at 12:36

( I believe you have created your module already but missing DB table part ) To create table for your module you need to have file Learner\Blog\Setup\InstallSchema.php:

  namespace Learner\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     
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    $installer = $setup;


    $table = $installer->getConnection()
            ['identity' => true, 'nullable' => false, 'primary' => true],
            'Post ID'
        ->addColumn('url', 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 Active?')
        ->addColumn('creation_date', Table::TYPE_DATETIME, null, ['nullable' => false], 'Creation Date')
        ->addColumn('update_date', Table::TYPE_DATETIME, null, ['nullable' => false], 'Update Date')
        ->addIndex($installer->getIdxName('blog_post', ['url_key']), ['url_key'])
        ->setComment('Magento 2 Blog');




here is nice tutorial which will guide you to develop module from scratch with configurations, save post(s) in table and view.



Please Follow the following steps:

create file app/code/Vendor/ModuleName/registration.php



create file app/code/Vendor/ModuleName/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_ModuleName" setup_version="1.0.1">

create file app\code\Vendor\ModuleName\Model\ModuleName.php


namespace Vendor\ModuleName\Model;

class ModuleName extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
    const CACHE_TAG = 'cache_tag';

    protected $_cacheTag = 'cache_tag';

    protected $_eventPrefix = 'cache_tag';

    protected function _construct()

    public function getIdentities()
        return [self::CACHE_TAG . '_' . $this->getId()];

    public function getDefaultValues()
        $values = [];

        return $values;

create file app\code\Vendor\ModuleName\Model\ResourceModel\ModuleName.php


    namespace Vendor\ModuleName\Model\ResourceModel;

    class ModuleName extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb

        public function __construct(
            \Magento\Framework\Model\ResourceModel\Db\Context $context
        ) {

    protected function _construct()
        $this->_init('table_name', 'table_primary_key');


create file app\code\Vendor\ModuleName\Model\ResourceModel\ModuleName\Collection.php

namespace Vendor\ModuleName\Model\ResourceModel\ModuleName;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
    protected $_idFieldName = 'table_primary_key';
    protected $_eventPrefix = 'table_collection';
    protected $_eventObject = 'table_collection';

     * Define resource model
     * @return void
    protected function _construct()
        $this->_init('Vendor\ModuleName\Model\ModuleName', 'Vendor\ModuleName\Model\ResourceModel\ModuleName');

Then step to get table data :

//Get Object Manager Instance
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$tableData = $objectManager->create('Vendor\ModuleName\Model\ModuleName')->getCollection();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.