5

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

6

You can load the Data by following below steps.

  1. Create a Block file or if you already created a block for your template use below code in the block in your module.

  2. 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();
    }
    
  3. use get getCollection in your template ( $block->getCollection() )

  4. Loop through each of the collection result.
  5. Assume you know how to create Block and template.

Hope this answer help you.

4
  • 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
    Jul 27, 2016 at 7:04
  • Using your answer and following alanstorm article, I got the solution. Thank you.
    – vedu
    Aug 2, 2016 at 12:30
  • is this $modelNameFactory and _modelFactory same?
    – Mujahidh
    Sep 21, 2017 at 5:22
  • 1
    @Mujahidh yes, we are using class object by $this->_modelFactory passing into constructor. Sep 21, 2017 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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