Magento 2 – How to Save Data to Custom Table Using Custom Model

magento2model

I have a custom table and I have written a custom model for it too, but I am not able to understand how can I perform something like Mage::getModel('')->setData(). I have followed the required structure, created the Model class and specified the resource model and defined the collection class, I can retrieve the data in the admin grid. But, still I am not able to understand how can I make use of my model to setData() and getData(). Here is my controller, to which I am making an AJAX call and I want to save my data to my custom table.

Rent.php

<?php

namespace Mofosys\Fastcure\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Vendor\Module\Model\ModuleFactory;

class Rent extends Action {

    protected $request;
    protected $_moduleFactory;

    public function __construct(Context $context, moduleFactory $moduleFactory) {
        $this->_moduleFactory = $moduleFactory;
        parent::__construct($context);
    }

    public function execute() {
        $model = $this->_moduleFactory->create();
        $data = $this->getRequest()->getPost();
        $model->setName($data['name']);
        $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultJson->setData($data['name']);
        return $resultJson;
    }
}

My models are in the following structure:

|-Model
|-ResourceModel
| |-Module
| | |-Collection.php
| |-Module.php
|-Module.php

Now, I am not able to understand that how can I insert this data in the table from my controller using my custom model.

Module/Model/Module.php

<?php
namespace Vendor\Module\Model;

use Magento\Framework\Exception\LocalizedException as CoreException;

class Fastcure extends \Magento\Framework\Model\AbstractModel {

    public function _construct() {
        $this->_init('Vendor\Module\Model\ResourceModel\Module');
    }

    public function getName() {
        return $this->getData(self::name);
    }

    public function setName($name) {
        return $this->setData(self::name, $name);
    }
}

All of this, doesn't seem to work, please help me out here guys.

Best Answer

The most simple solution for your own model is to get factory using di and then create 1 copy (or load it), fill it in with data and save. Here is the example with the model called Module:

class Rent extends Action {

    protected $request;
    protected $_moduleFactory;

    public function __construct(Context $context, moduleFactory $moduleFactory) {
        $this->_moduleFactory = $moduleFactory;
        parent::__construct($context);
    }

    public function execute() {
        $model = $this->_moduleFactory->create();
        $data = $this->getRequest()->getPost();
        if (isset($data['id'])) {
            $model->load($data['id']);
        }

        $model->setData($data);
        $model->setName($data['name']);
        $model->save(); // now deprecated, but works fine if you have no repository
        $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
        $resultJson->setData($data['name']);
        return $resultJson;
    }
}
Related Topic