5

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.

3
  • Add your code for more information, you can not use Mage::getModel in magento-2. You have to save data by object manager in magento-2. Oct 3, 2016 at 7:12
  • In Magento 2 you get access to your objects by dependency injection. Then you can setData() in the same way you do in M1. Oct 3, 2016 at 7:30
  • @S H Patel, I have updated my code, please let me know how can I save my data to the custom table from my custom model. Oct 3, 2016 at 12:51

2 Answers 2

12

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;
    }
}
4
  • 1
    I want to insert a new row Oct 4, 2016 at 4:18
  • @AbhishekDhanrajShahdeo this code will create a new entry, if an id hasn't been sent.
    – Mageworx
    Oct 6, 2016 at 7:54
  • I had to use $data = $this->getRequest()->getPostValue(); Oct 20, 2016 at 2:32
  • It gives error like : "Controller\\Save\\moduleFactory does not exist\" Dec 6, 2019 at 11:35
-20

If you want to insert data, you can try inserting Custom Query.

$this->_resources = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\ResourceConnection');
$connection= $this->_resources->getConnection();

$themeTable = $this->_resources->getTableName('yourtablename');
$sql = "INSERT INTO " . $themeTable . "(field1, field2) VALUES ('1', 'NameABC')";
$connection->query($sql);
4
  • 6
    No, don't want to use direct queries at all. Oct 3, 2016 at 10:55
  • Not Recommended. Jan 9, 2019 at 5:11
  • never use object manager, it is considered bad practice
    – Baber
    May 14, 2020 at 8:42
  • It looks like he is passing static queries with objectmanger :( which is very very bad for your site health. Feb 10, 2022 at 16:49

Your Answer

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

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