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.

  • 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. – Suresh Chikani Oct 3 '16 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. – Florin-Adrian Serbu Oct 3 '16 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. – Abhishek Dhanraj Shahdeo Oct 3 '16 at 12:51
11

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

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);
| improve this answer | |
  • 5
    No, don't want to use direct queries at all. – Abhishek Dhanraj Shahdeo Oct 3 '16 at 10:55
  • Not Recommended. – Naveed Ramzan Jan 9 '19 at 5:11
  • never use object manager, it is considered bad practice – Baber May 14 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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