How to create a Model in magento 2

Maybe you know that it needs to register a model in file config.xml in magento 1. Now, with magento 2, it will be easier more than. We only need to create two files:

app/code/Mageoneplus/Rewardcredit/Model/Rule.php:

<?php
namespace Mageoneplus\Rewardcredit\Model;


class Rule extends \Magento\Framework\Model\AbstractModel
{
    const EARNING = 1;
    const SPENDING = 2;
    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Mageoneplus\Rewardcredit\Model\ResourceModel\Rule');
    }
}

app/code/Mageoneplus/Rewardcredit/Model/ResourceModel/Rule.php:

<?php
namespace Mageoneplus\Rewardcredit\Model\ResourceModel;

class Rule extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('rewardcredit_rule', 'id');
    }

}

Now, you can use modelĀ app/code/Mageoneplus/Rewardcredit/Model/Rule.php to create or edit a row in the table “rewardcredit_rule”

Add Comment

Required fields are marked *. Your email address will not be published.