4

When I try to create a new record in my custom module I get the below mentioned error

Something went wrong while saving the item.Notice: Array to string conversion in /opt/lampp/htdocs/Magento225/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3028

Admin field in Ui Component form

  <field name="customer_group_ids">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Customer Groups</item>
            <item name="dataType" xsi:type="string">number</item>
            <item name="formElement" xsi:type="string">multiselect</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
            <item name="source" xsi:type="string">catalog_rule</item>
            <item name="dataScope" xsi:type="string">customer_group_ids</item>
        </item>
        <item name="options" xsi:type="object">Magento\CatalogRule\Model\Rule\CustomerGroupsOptionsProvider</item>
    </argument>
</field>

Error is coming form the piece of code below , when I comment out this code it works fine.

      if (!empty($data['customer_group_ids'])) {

            $data['customer_group_ids'] = implode(',', $data['customer_group_ids']);
        } else {
            $data['customer_group_ids'] = '';
        }

Below code is similar to the one mention above(customer_group_ids) and working fine and saves the records in database correctly .

        if (!empty($data['cmspage_ids'])) {
            $data['cmspage_ids'] = implode(',', $data['cmspage_ids']);
        }else {
            $data['cmspage_ids'] = '';
        }

I have both the above pieces of code in my save.php one is working fine and the other is not! What causes this issue to happen?

Output of the Array:

Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
  • May I know at which point you are adding record? – aman Sep 14 '18 at 6:52
  • there is a single table , directly from Controller\Adminhtml\Item\Save.php – Verdu Sep 14 '18 at 6:57
1

please check type of $data['customer_group_ids']

using var_dump($data['customer_group_ids']);

$data['customer_group_ids'] should be array

  • Error is not thrown during implode(',', $data['customer_group_ids']); this error comes when we $model->save(); – Verdu Sep 14 '18 at 6:48
1

Please replace your code with this and try

if (is_array($data['customer_group_ids'])) {
    $data['customer_group_ids'] = implode(',', $data['customer_group_ids']);
}else {
    $data['customer_group_ids'] = '';
}

or try this

$yourObject->setData('customer_group_ids',implode(',', $data['customer_group_ids']));

As here I see something strange, your $data['customer_group_ids'] is Array but you are change the array to string and saving so not sure if you are doing this correct or not.

  • I tried both of your suggested solutions , but the result remains the same. – Verdu Sep 14 '18 at 11:01

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.