I have created a custom module : Ved_Mymodule
File structure of the module is as follows:
Ved
└───Mymodule
│ registration.php
│
├───etc
│ module.xml
│
├───Model
│ │ News.php
│ │
│ └───Resource
│ │ News.php
│ │
│ └───News
│ Collection.php
│
├───Setup
│ InstallSchema.php
│ UpdateSchema.php
│
└───view
└───frontend
│ requirejs-config.js
│
├───layout
│ deafult.xml
│
└───web
├───css
└───js
customCatalogAddToCart.js
InstallSchema.php
Using this module, one table with name ved_mymodule is created. 'id' is the primary key in that column.
Here are the files :
Ved\Mymodule\Model\News.php :
<?php
namespace Ved\Mymodule\Model;
use Magento\Framework\Model\AbstractModel;
class News extends AbstractModel
{
/**
* Define resource model
*/
protected function _construct()
{
$this->_init('Ved\Mymodule\Model\Resource\News');
}
}
Ved\Mymodule\Model\Resource\News.php:
<?php
namespace Ved\Mymodule\Model\Resource;
use Magento\Framework\Model\Resource\Db\AbstractDb;
class News extends AbstractDb
{
/**
* Define main table
*/
protected function _construct()
{
$this->_init('ved_mymodule', 'id');
}
}
Ved\Mymodule\Model\Resource\News\Collection.php:
<?php
namespace Ved\Mymodule\Model\Resource\News;
use Magento\Framework\Model\Resource\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
/**
* Define model & resource model
*/
protected function _construct()
{
$this->_init(
'Ved\Mymodule\Model\News',
'Ved\Mymodule\Model\Resource\News'
);
}
}
Best Answer
You can load the Data by following below steps.
Create a Block file or if you already created a block for your template use below code in the block in your module.
Add below code to block
use get
getCollection
in your template ( $block->getCollection() )Hope this answer help you.