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– Suresh Chikani2016年10月03日 07:12:21 +00:00Commented 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.Florin-Adrian Serbu– Florin-Adrian Serbu2016年10月03日 07:30:09 +00:00Commented 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.Abhishek Dhanraj Shahdeo– Abhishek Dhanraj Shahdeo2016年10月03日 12:51:54 +00:00Commented Oct 3, 2016 at 12:51
2 Answers 2
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;
}
}
-
1I want to insert a new rowAbhishek Dhanraj Shahdeo– Abhishek Dhanraj Shahdeo2016年10月04日 04:18:35 +00:00Commented Oct 4, 2016 at 4:18
-
@AbhishekDhanrajShahdeo this code will create a new entry, if an id hasn't been sent.Mageworx– Mageworx2016年10月06日 07:54:01 +00:00Commented Oct 6, 2016 at 7:54
-
I had to use
$data = $this->getRequest()->getPostValue();Xenocide8998– Xenocide89982016年10月20日 02:32:13 +00:00Commented Oct 20, 2016 at 2:32 -
It gives error like : "Controller\\Save\\moduleFactory does not exist\"Chirag Parmar– Chirag Parmar2019年12月06日 11:35:50 +00:00Commented Dec 6, 2019 at 11:35
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);
-
6No, don't want to use direct queries at all.Abhishek Dhanraj Shahdeo– Abhishek Dhanraj Shahdeo2016年10月03日 10:55:25 +00:00Commented Oct 3, 2016 at 10:55
-
Not Recommended.Naveed Ramzan– Naveed Ramzan2019年01月09日 05:11:26 +00:00Commented Jan 9, 2019 at 5:11
-
never use object manager, it is considered bad practiceBaber– Baber2020年05月14日 08:42:08 +00:00Commented 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.Zahid Hussain Magento– Zahid Hussain Magento2022年02月10日 16:49:56 +00:00Commented Feb 10, 2022 at 16:49