How To Use Model In Magento 2

In this tutorial, we will learn about How To Use Model in Magento 2.

This is a basic tutorial followed by How To Create Database Table in Magento 2. You should see the previous article to understand this lesson.

Module File Structure

We updated our module file structure looks as follows:

module file structure

Create the Model

You create this file according to the path: Magetop/Helloworld/Model/Posts.php.

<?php
namespace Magetop\Helloworld\Model;

use Magento\Framework\Model\AbstractModel;

class Posts extends AbstractModel
{
    protected function _construct()
    {
        $this->_init('Magetop\Helloworld\Model\ResourceModel\Posts');
    }
}

Next, you create Posts.php in ResourceModel folder according to the path: Magetop/Helloworld/Model/ResourceModel/Posts.php.

<?php
namespace Magetop\Helloworld\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Posts extends AbstractDb
{
    protected function _construct()
    {
        // magetop_blog is table name and id is Primary of Table
        $this->_init('magetop_blog', 'id');
    }
}

Then, you create Collection.php according to the path: Magetop/Helloworld/Model/ResourceModel/Posts/Collection.php.

<?php
namespace Magetop\Helloworld\Model\ResourceModel\Posts;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(
            'Magetop\Helloworld\Model\Posts',
            'Magetop\Helloworld\Model\ResourceModel\Posts'
        );
    }
}

So we have created the Model with the 3 Class above.

Get data from table database

As I said at the beginning of the article, you need to have a table database to get data. If you haven’t created it yet, please review the previous post.

Next, we will get data from the magetop_blog table in this database.

magetop_blog in database

Edit the file to the path: Magetop/Helloworld/Controller/Index/Index.php.

<?php
namespace Magetop\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magetop\Helloworld\Model\ResourceModel\Posts\CollectionFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_resultPageFactory;
    protected $_postsFactory;

    public function __construct(
        Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        CollectionFactory $postsFactory)
    {
        parent::__construct($context);
        $this->_resultPageFactory = $resultPageFactory;
        $this->_postsFactory = $postsFactory;
    }

    public function execute()
    {
        echo "Get Data From magetop_blog table";
        $this->_postsFactory->create();
        $collection = $this->_postsFactory->create()
            ->addFieldToSelect(array('title','description','created_at','status'))
            ->addFieldToFilter('status',1)
            ->setPageSize(10);
        echo '<pre>';
        print_r($collection->getData());
        echo '<pre>';
    }
}

Do not forget to run the command php bin/magento setup:upgrade after fixing in the __construct () function.

You access the link created on the store page to check it out.

<yourdomain>/helloworld/index/index
get data from database

I hope through this series you can create your own complete module. Good luck!

In addition to How To Use Model in Magento 2, you can read the articles How To Get Items To Storefront In Magento 2.

Follow us for the more helpful article!

We hope this is a useful series for you.

Thank you for reading!

5 2 votes
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x