0

How to make all 0(zero) values in my custom grid into Null or Blank? because when I edit and I leave a field blank it automatically make column values 0 once I save it.

enter image description here

here's the code of my inline edit:

protected $jsonFactory;

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
) {
    parent::__construct($context);
    $this->jsonFactory = $jsonFactory;
}

public function execute()
{
    /** @var \Magento\Framework\Controller\Result\Json $resultJson */
    $resultJson = $this->jsonFactory->create();
    $error = false;
    $messages = [];

    if ($this->getRequest()->getParam('isAjax')) {
        $postItems = $this->getRequest()->getParam('items', []);
        if (!count($postItems)) {
            $messages[] = __('Please correct the data sent.');
            $error = true;
        } else {
            foreach (array_keys($postItems) as $modelid) {
                /** @var \Magento\Cms\Model\Block $block */
                $model = $this->_objectManager->create('Cumbria\EventsRegistration\Model\Events')->load($modelid);
                try {
                    $model->setData(array_merge($model->getData(), $postItems[$modelid]));
                    $model->save();
                } catch (\Exception $e) {
                    $messages[] = "[Events ID: {$modelid}]  {$e->getMessage()}";
                    $error = true;
                }
            }
        }
    }

    return $resultJson->setData([
        'messages' => $messages,
        'error' => $error
    ]);
}

XML for item listing:

<column name="order_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
                <item name="filter" xsi:type="string">text</item>
                <item name="editor" xsi:type="array">
                    <item name="editorType" xsi:type="string">text</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                </item>                    
                <item name="label" xsi:type="string" translate="true">Order ID</item>
            </item>
        </argument>
    </column>
  • I think it was due to the fact that ids in Magento are integer – fmsthird Nov 21 '19 at 7:08
1

make sure that the table field displayed in your grid supports null values.
After you do that, you can just run the query UPDATE table set field = null where field = 0

| improve this answer | |
  • additionally make the table column nullable true, so when there is no value inserted then default value will be NULL. – Mohammad Mujassam Nov 22 '19 at 10:28

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.