I have created an admin grid and also created an associated form for that grid. And then I have created a table for that grid. Now, how to save the form data that particular grid DB table
Magento – How to save backend form data in database in magento 2
backendgridmagento2.2PHP
Related Solutions
I don't have a full explanation, but I have an idea.
All elements rendered with mage-init
will/should be handled by some javascript code.
You are linking these buttons to a form, and the form is handled by this js file lib/web/mage/backend/form.js
where a jquery ui widget is created.
These are the options for the widget
options: {
handlersData: {
save: {},
saveAndContinueEdit: {
action: {
args: {
back: 'edit'
}
}
},
preview: {
target: '_blank'
}
}
},
you can see a saveAndContinueEdit
in there somewhere inside handlersData
.
Looking for the usages of handlersData
you end up in _beforeSubmit
where some magic happens (I don't really understand everything in there), and at one point _processData
is called.
Moving to _processData
you will see something like this
if (attrName === 'action') {
data[attrName] = this._getActionUrl(attrValue);
}
this means that the action
of the form is changed based on the button pressed.
the _getActionUrl
function looks like this
_getActionUrl: function(data) {
if ($.type(data) === 'object') {
return this._buildURL(this.oldAttributes.action, data.args);
} else {
return $.type(data) === 'string' ? data : this.oldAttributes.action;
}
},
you can see in there data.args
involved. The same variable in the widget options for saveAndContinueEdit
.
Conclusion: When you set the role saveAndContinueEdit
toa submit button, the action of the form is changed via js and back/edit
is added to the url.
on_click
is transformed to the onclick
event and simply called.
I honestly have no idea why there are 2 ways of doing this. Maybe the delete
actions didn't get refactored yet.
if ($flag === true) {
$fieldset->addField(
'logo_image', 'label', [
'name' => 'logo_image',
'label' => __('Logo Image'),
'title' => __('Logo Image'),
'renderer' => '[Vendor]\[Module Name]\Block\Adminhtml\Quotation\Renderer\LogoImage',
]
);
}
In LogoImage.php
<?php
namespace [Vendor]\[Module Name]\Block\Adminhtml\Quotation\Renderer;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\Object;
use Magento\Store\Model\StoreManagerInterface;
class LogoImage extends AbstractRenderer
{
private $_storeManager;
/**
* @param \Magento\Backend\Block\Context $context
* @param array $data
*/
public function __construct(\Magento\Backend\Block\Context $context,
StoreManagerInterface $storemanager, array $data = [])
{
$this->_storeManager = $storemanager;
parent::__construct($context, $data);
$this->_authorization = $context->getAuthorization();
}
/**
* Renders grid column
*
* @param Object $row
* @return string
*/
public function render(Object $row)
{
$mediaDirectory = $this->_storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
);
$imageUrl = $mediaDirectory.'cart2quote/images/'.$this->_getValue($row);
return '<img src="'.$imageUrl.'" width="50"/>';
}
}
Best Answer
You have to make a model corresponding to the table, from which you will be able to do all db related stuff.
Following is a good tutorial on how to make model ; https://www.mageplaza.com/magento-2-module-development/how-to-create-crud-model-magento-2.html