3

I've been trying to save Multi Select field values to DB.

I've added the field to sales rule form using ui_component.

<fieldset name="exclude_brands">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Exclude Brands</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">15</item>
        </item>
    </argument>
    <field name="start_date" formElement="date">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">sales_rule</item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="validate-date" xsi:type="boolean">true</rule>
            </validation>
            <dataType>text</dataType>
            <label translate="true">Start Date</label>
            <visible>true</visible>
            <dataScope>start_date</dataScope>
        </settings>
    </field>
    <field name="end_date" formElement="date">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">sales_rule</item>

            </item>
        </argument>
        <settings>
            <validation>
                <rule name="validate-date" xsi:type="boolean">true</rule>
            </validation>
            <dataType>text</dataType>
            <label translate="true">End Date</label>
            <visible>true</visible>
            <dataScope>end_date</dataScope>
        </settings>
    </field>
    <field name="brand_ids" formElement="select">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">sales_rule</item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="validate-brands" xsi:type="boolean">true</rule>
            </validation>
            <dataType>varchar</dataType>
            <label translate="true">Brand List</label>
            <dataScope>brand_ids</dataScope>
        </settings>
        <formElements>
            <select>
                <settings>
                    <options class="Vendor\Module\Block\SalesRule\BrandList"/>

                </settings>
            </select>
        </formElements>
    </field>
</fieldset>

To save this field I've created a plugin with after execute method.

<type name="Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Save">
    <plugin name="save_brands" type="Vendor\Module\Model\Plugin\brands" sortOrder="1"/>
</type>

Vendor/Module/Model/Plugin/Brands.php

public function afterexecute(\Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Save $save,$result)
{

    $ruleId =  $save->getRequest()->getParam('rule_id'); 

    $brandIds = implode(',',$save->getRequest()->getParam('brand_ids'));

    try{
        $salesRuleData =  $this->salesRule->load($ruleId);
        $salesRuleData->setStartDate($save->getRequest()->getParam('start_date'));
        $salesRuleData->setEndDate($save->getRequest()->getParam('end_date'));
        $salesRuleData->setBrandIds($brandIds);
        $salesRuleData->save();         
    }
    catch(Exception $e)
    {
        echo $e->getMessage();  
    }

}

Now, issue is field value is saving to DB but, Getting exception like below.

Something went wrong while saving the rule data. Please review the error log.

Check Below Error Log.

(Exception(code: 0): Notice: Array to string conversion in C:\\xampp\\htdocs\\mage2\\vendor\\magento\\zendframework1\\library\\Zend\\Db\\Statement\\Pdo.php on line 228 at C:\\xampp\\htdocs\\mage2\\vendor\\magento\\framework\\App\\ErrorHandler.php:61)"} []

Anyone facing same issues before. Really need help with this.

Thanks in Advance.

6
  • $salesRuleData->setBrandIds($brandIds); check for $brandids data type. they are in array or string Commented Apr 6, 2018 at 14:19
  • @Rizwan, It returns array, So i converted into string and than set it to its object. Commented Apr 7, 2018 at 4:24
  • Please comment this line $salesRuleData->setBrandIds($brandIds); and check that after comment this line still exception is generated while saving to DB ?. Commented Apr 7, 2018 at 7:37
  • Yes, I did that, Same Exception while save if i select value from multiselect. Commented Apr 7, 2018 at 7:55
  • If after comment above line still you are getting same exception then there is no error in your ui_component code.Please check your plugin. Commented Apr 7, 2018 at 11:17

2 Answers 2

2

I don't know why plugin doesn't work with current scenario.

If anyone tell Why plugin not worked with this scenario ?

So, I used event called adminhtml_controller_salesrule_prepare_save to save my fields value to DB.

Here is full code.

Vendor/Module/etc/adminhtml/events.xml

<event name="adminhtml_controller_salesrule_prepare_save">
    <observer name="save_brands" 
        instance="Vendor\Module\Observer\SaveExcludedBrands" />
</event>

Than, Create observer file within your module.

Vendor/Module/Observer/SaveExcludedBrands.php

public function execute(Observer $observer)
{
    $data = $observer->getRequest()->getPostValue();
    //print_r($data); exit;
    $request = $observer->getEvent()->getRequest();

    $brands = implode(',',$data['brands']);

    $startDate = $this->timeZoneInterface->date($data['start_date'])->format('Y-m-d'); 
    $endDate = $this->timeZoneInterface->date($data['end_date'])->format('Y-m-d');


    $request->setPostValue('brands',$brands);
    $request->setPostValue('start_date',$startDate);
    $request->setPostValue('end_date',$endDate);
}

Now, No exception and notice and its working.

0

Can you try this

public function afterexecute(\Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Save $save,$result)
{

    $ruleId =  $save->getRequest()->getParam('rule_id'); 

  $brandIds = implode(',',$save->getRequest()->getParam('brands'));

    try{
        $salesRuleData =  $this->salesRule->load($ruleId);
        $salesRuleData->setStartDate($save->getRequest()->getParam('start_date'));
        $salesRuleData->setEndDate($save->getRequest()->getParam('end_date'));
        $salesRuleData->setBrands($brandIds);
        $salesRuleData->save();         
    }
    catch(Exception $e)
    {
        echo $e->getMessage();  
    }

}
11
  • I've already tried your way before but same Notice Error. Commented Apr 11, 2018 at 4:28
  • What column type use in table Commented Apr 11, 2018 at 4:38
  • If you use single brand id. Commented Apr 11, 2018 at 4:45
  • Same issue, Values saving to table but notice error there. Commented Apr 11, 2018 at 5:09
  • If use $salesRuleData->setBrands(4); , Means if i set static value than its working but when dynamic values come than it started throwing notice error. Dyamic it returns array , I did try both way directly set array OR by implode it But both not working. Also used $salesRuleData->setData('brands',$brands); $salesRuleData->updateData($salesRuleData); way to set brands but no luck Commented Apr 11, 2018 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.