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'
);
}
}
1 Answer 1
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
public function __construct( Context $context, \Ved\Mymodule\Model\NewsFactory $modelNameFactory, array $data = array() ) { $this->_modelFactory = $modelFactory; parent::__construct($context, $data); } public function getCollection(){ return $this->_modelFactory->create()->getCollection(); }use get
getCollectionin your template ( $block->getCollection() )- Loop through each of the collection result.
- Assume you know how to create Block and template.
Hope this answer help you.
-
I have not created a block and template. First I need to try this then I will let you know. Thank you for this.vedu– vedu2016年07月27日 07:04:06 +00:00Commented Jul 27, 2016 at 7:04
-
-
is this $modelNameFactory and _modelFactory same?Mujahidh– Mujahidh2017年09月21日 05:22:07 +00:00Commented Sep 21, 2017 at 5:22
-
1@Mujahidh yes, we are using class object by $this->_modelFactory passing into constructor.Krishna ijjada– Krishna ijjada2017年09月21日 06:32:03 +00:00Commented Sep 21, 2017 at 6:32