How to save data to custom table using custom model in magento2

12,198
|-Controller 
    |-Index
        |-Rent.php
|-Model
   |-ResourceModel
   |    |-Module
   |    |    |-Collection.php
   |    |-Module.php
   |-Module.php

-------- controller code Rent.php-----------------------

<?php 
namespace Mofosys\Fastcure\Controller\Index;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class Rent extends \Magento\Framework\App\Action\Action
{

    public function execute()
    { 

        $data = $this->getRequest()->getPost();

        $model->setName($data['name']);

        $model = $objectManager->create('Mofosys\Fastcure\Model\Module');

        $model->setData('itemname', $data['name']);  

        $msg =""; 

        if($data != ''){ 
            $model->save(); 
            $msg = 'saved successfully';
        }else{ 
             $msg = 'not saved';  
        } 
        echo $msg;
    }

}  

--------------------model code Module.php---------------------------

<?php

namespace Mofosys\Fastcure\Model;

use Magento\Framework\Phrase;

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

    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
        )
        {
            parent::__construct($context, $registry, $resource, $resourceCollection, $data);
        }
        protected function _construct()
        { 
            $this->_init('Mofosys\Fastcure\Model\ResourceModel\Module');
        } 
}
Share:
12,198
Abhishek Dhanraj Shahdeo
Author by

Abhishek Dhanraj Shahdeo

I like to code, love to help people out and just keep learning!

Updated on July 17, 2022

Comments

  • Abhishek Dhanraj Shahdeo
    Abhishek Dhanraj Shahdeo almost 2 years

    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.

  • luenib
    luenib almost 5 years
    This guideline is very helpful and used it to add a custom table to my DB. But now I realize that I need another table. Do I have to this all over again but now as Model\ResourceModel-2? What I do know is that I will need a new Collection, InstallData, and InstallSchema.