Magento 2 save comma separated values IN Table
In This Post We will create a custom table for a grid using block file and db schema(declarative schema). let's begin
db_schema.xml file
To create a custom table in a database you have to define a declarative schema first.
To define a Dbschema create a new file:
app/code/Vendor/Mymodue/etc/db_schema.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Created By : Isev_BannerSlider Pvt. Ltd.
*/
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="accessory" resource="default" engine="innodb" comment="Banner Slider">
<column xsi:type="int" name="id" padding="7" unsigned="false" nullable="false" identity="true" comment="ID" />
<column xsi:type="varchar" name="aname" nullable="false" length="20" comment="Title" />
<column xsi:type="text" name="quantity" nullable="false" comment=" Quantity" />
<column xsi:type="varchar" name="price" nullable="false" length="255" comment="Price" />
<column xsi:type="varchar" name="pimage" nullable="false" comment="Pimage" />
<column xsi:type="varchar" name="customer_status" nullable="false" length="20" comment="Customer Status" />
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="id" />
</constraint>
</table>
</schema>
To create a form grid you have to define a block file first.
To define a Dbschema create a new file:
app/code/Vendor/Mymodue/Block/Adminhtml/Edit/Form.php
namespace Vendor\Extension\Block\Adminhtml\Edit;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use Magento\Framework\Data\FormFactory;
use Vendor\Extension\Block\Adminhtml\Status;
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
protected $_systemStore;
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
Status $options,
array $data = []
)
{
$this->options = $options;
parent::__construct($context, $registry, $formFactory, $data);
}
protected function _prepareForm()
{
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
$model = $this->_coreRegistry->registry('row_data');
$form = $this->_formFactory->create(
['data' => [
'id' => 'edit_form',
'enctype' => 'multipart/form-data',
'action' => $this->getData('action'),
'method' => 'post'
]
]
);
$form->setHtmlIdPrefix('mtgrid_');
if ($model->getId()) {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Edit Row Data'), 'class' => 'fieldset-wide']
);
$fieldset->addField('id', 'hidden', ['name' => 'id']);
} else {
$fieldset = $form->addFieldset(
'base_fieldset',
['legend' => __('Add Row Data'), 'class' => 'fieldset-wide']
);
}
$fieldset->addField(
'aname',
'text',
[
'name' => 'aname
'label' => __('Aname.'),
'id' => 'aname',
'title' => __('Aname.'),
'class' => '',
]
);
//$wysiwygConfig = $this->_wysiwygConfig->getConfig(['tab_id' => $this->getTabId()]);
$fieldset->addField(
'quantity',
'date',
[
'name' => 'quantity',
'label' => __('Quantity'),
'class' => '',
'class' => 'required-entry',
]
);
$fieldset->addField(
'price',
'text',
[
'name' => 'price',
'label' => __('Price'),
'id' => 'chnlmanager',
'title' => __('price'),
'class' => 'required-entry',
]
);
$fieldset->addField(
'pimage',
'select',
[
'name' => 'pimage',
'label' => __('PImg'),
'id' => 'pimage',
'title' => __('PImg'),
'values' => $this->options->getOptionArray(),
'class' => 'required-entry',
'required' => true,
]
);
$fieldset->addField(
'customer_status',
'select',
[
'name' => 'customer_status',
'label' => __('Customer Status'),
'id' => 'customer_status',
'title' => __('Customer Status'),
'values' => [0=>"NOT LOGGEDIN",1=>"Gernal",2=>"Retailer"],
'class' => 'Customer_status',
'required' => true,
]
);
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
save.php file
Now create an array or if you have then implode the array in a controller first.
To define a Controller create a new file:
app/code/Vendor/Mymodue/Controller/Grid/Save.php
$arr = array('Hello','World!','Beautiful','Day!');
$arr1 = implode(" ",$arr);
$data->SetColumnName($arr1);
$data->Save();

2 Comments
There is multiple select how can I fix it?
ReplyDeletechange "select" to "multiselect" after you can get an array.
Delete