0

I have created a custom module with a custom table, now I need to fetch the data from the table and display in the home page. I achieved it by applying object manager I know it's not the correct method can anyone please tell me how to do it.

thank you

  • create model.... – Rk Rathod Feb 11 at 6:55
  • created model and resource model also collection – jithin Feb 11 at 6:59
  • you can easily get table data using model – Rk Rathod Feb 11 at 6:59
  • can u help me in getting I am new to magento2 . – jithin Feb 11 at 7:01
  • check my answer – Rk Rathod Feb 11 at 7:01
3

1) I Assume you have created Model and Collection file associated with that tables.

2) In a Block PHP file constructor add one argument (Dependency Injection) like below and store it in a class member variable.

 public function __construct(
    Context $context,
    \Namespace\Modulename\Model\ModelNameFactory $modelNameFactory,

    array $data = array()
) {
    $this->_modelFactory = $modelFactory;
    parent::__construct($context, $data);
}

3) Prepare a public method in your block to access collection like below.

public function getCollection(){

    return $this->_modelFactory->create()->getCollection();

}

4) Loop through each of the collection result.

| improve this answer | |
  • I am a newbie so please help in doing the task – jithin Feb 11 at 7:14
1

app/code/VendoreName/ModuleName/Setup

InstallSchema.php

<?php

namespace VendoreName\ModuleName\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $table = $installer->getConnection()->newTable(
            $installer->getTable('custom_table_name')
        )->addColumn(
            'id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'nullable' => false, 'primary' => true],
            'Id'
        )->addColumn(
            'prd_entity_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false],
            'Product Entity ID'
        )->addColumn(
            'prd_sku',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false],
            'Product SKU'
        )->addColumn(
            'usr_name',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false],
            'User Name'
        )->addColumn(
            'email',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            ['nullable' => false],
            'Email Address'
        )->addColumn(
            'subject',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            '2M',
            ['nullable' => false],
            'Subject'
        )->addColumn(
            'msg',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            '2M',
            ['nullable' => false],
            'Massage'
        )->addColumn(
            'pubish_datetime',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            [
                'nullable' => false,
                'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT,
            ],
            'Publish Date Time'
        )->addColumn(
            'update_datetime',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            [],
            'Modification Date Time'
        )->setComment(
            'Custome Table'
        );
        $installer->getConnection()->createTable($table);
        $installer->endSetup();
    }
}

here column name are my you can put your column name

Now create an Modal

app/code/VendoreName/ModuleName/Model

Customtable.php

<?php

namespace VendoreName\ModuleName\Model;

use VendoreName\ModuleName\Model\ResourceModel\Customtable as CustomtableResourceModel;

class Customtable extends \Magento\Framework\Model\AbstractModel
{
    const CACHE_TAG = 'custom_table_name';

    protected $_cacheTag = 'custom_table_name';
    protected $_eventPrefix = 'custom_table_name';

    protected function _construct()
    {
        $this->_init(CustomtableResourceModel::class);
    }

}

app/code/VendoreName/ModuleName/Model/ResourceModel

Customtable.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel;

class Customtable extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected $_idFieldName = 'id';
    protected $_date;

    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
        \Magento\Framework\Stdlib\DateTime\DateTime $date,
        $resourcePrefix = null
    ) {
        parent::__construct($context, $resourcePrefix);
        $this->_date = $date;
    }
    protected function _construct()
    {
        $this->_init('custom_table_name', 'id');
    }
}

app/code/VendoreName/ModuleName/Model/ResourceModel/Customtable

Collection.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel\Customtable;

use VendoreName\ModuleName\Model\Customtable as CustomtableModel;
use VendoreName\ModuleName\Model\ResourceModel\Customtable as CustomtableResourceModel;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected $_idFieldName = 'id';

    protected function _construct()
    {
        $this->_init(
            CustomtableModel::class,
            CustomtableResourceModel::class
        );
    }
}

crate an block file

app/code/VendoreName/ModuleName/Block

CustomeBlock.php

<?php

namespace VendoreName\ModuleName\Block;

use Magento\Framework\App\Request\DataPersistorInterface;

class CustomeBlock extends \Magento\Framework\View\Element\Template
{
    protected $customData;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \VendoreName\ModuleName\Model\ResourceModel\Customtable\CollectionFactory $customData
    ) {
        parent::__construct($context);
        $this->customData = $customData;
    }
    public function getCustomTableData()
    {
        return $this->customData->create();
    }
}

add this into phtml file

app/code/VendoreName/ModuleName/view/frontend/templates

customefile.phtml

<?php

print_r($block->getCustomTableData()->getData());

?>
<h1> Call phtml file</h1>

app/code/VendoreName/ModuleName/view/frontend/layout

cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="VendoreName\ModuleName\Block\CustomeBlock" template="VendoreName_ModuleName::customefile.phtml"/>
        </referenceContainer>
    </body>
</page>

after that run magento command

php bin/magento s:up
php bin/magento s:s:d
php bin/magento c:c

Note: please check your custome table in database and also check your Module name in setup_module table.

| improve this answer | |

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.