0

When i tried to edit an image in custom module i am getting an error like below.

1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in /var/www/html/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3101

I have a fix for this. replacing this code set

case 'longtext':
$value  = (string)$value;
if ($column['NULLABLE'] && $value == '') {
    $value = null;
}
break;

With

case 'longtext':
if(!is_array($value)) $value  = (string)$value;
else $value = '';
if ($column['NULLABLE'] && $value == '') {
    $value = null;
}
break;

I want to know how can i do this in proper way rather than directly editing the core file. UPDATE My modules Save.php

try{
    $uploader = $this->_objectManager->create(
                'Magento\MediaStorage\Model\File\Uploader',
                ['fileId' => 'image']
            );
    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
            /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
    $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
    $uploader->setAllowRenameFiles(true);
    $uploader->setFilesDispersion(true);
    /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
    $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
                ->getDirectoryRead(DirectoryList::MEDIA);
    $result = $uploader->save($mediaDirectory->getAbsolutePath('news_news'));
    if($result['error']==0)
    {
        $data['image'] = 'news_news' . $result['file'];
    }
  • Hi Muja, Override this class file using the preferences in magento2 – Vishnunath Sep 3 at 7:08
  • @Vishnunath can you pls provide me an answer explaining? – Mujahidh Sep 3 at 7:14
  • This is nothing to do with the magento/framework/DB/Adapter/Pdo/Mysql.php file its to do with your custom module. for image you are passing the standard array for image upload which contains fiile name and url you need to split the data or serialize it in your save action – Dava Gordon Sep 3 at 7:16
  • share custom module code. No need to edit the core. – Qaisar Satti Sep 3 at 7:22
1

Create a new module

Add etc/di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

       <preference for="Magento\Framework\DB\Adapter\Pdo\Mysql" type="Vendo\Module\DB\Adapter\Pdo\Mysql" />

    </config>

Add Mysql.php into the DB/Adapter/Pdo

   <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */

    namespace Vendor\Magento\DB\Adapter\Pdo;




    class Mysql extends Magento\Framework\DB\Adapter\Pdo\Mysql
    {
        public function prepareColumnValue(array $column, $value)
        {
            if ($value instanceof \Zend_Db_Expr) {
                return $value;
            }
            if ($value instanceof Parameter) {
                return $value;
            }

            // return original value if invalid column describe data
            if (!isset($column['DATA_TYPE'])) {
                return $value;
            }

            // return null
            if ($value === null && $column['NULLABLE']) {
                return null;
            }

            switch ($column['DATA_TYPE']) {
                case 'smallint':
                case 'int':
                    $value = (int)$value;
                    break;
                case 'bigint':
                    if (!is_integer($value)) {
                        $value = sprintf('%.0f', (float)$value);
                    }
                    break;

                case 'decimal':
                    $precision  = 10;
                    $scale      = 0;
                    if (isset($column['SCALE'])) {
                        $scale = $column['SCALE'];
                    }
                    if (isset($column['PRECISION'])) {
                        $precision = $column['PRECISION'];
                    }
                    $format = sprintf('%%%d.%dF', $precision - $scale, $scale);
                    $value  = (float)sprintf($format, $value);
                    break;

                case 'float':
                    $value  = (float)sprintf('%F', $value);
                    break;

                case 'date':
                    $value  = $this->formatDate($value, false);
                    break;
                case 'datetime':
                case 'timestamp':
                    $value  = $this->formatDate($value);
                    break;

                case 'varchar':
                case 'mediumtext':
                case 'text':
                case 'longtext':
    if(!is_array($value)) $value  = (string)$value;
    else $value = '';
    if ($column['NULLABLE'] && $value == '') {
        $value = null;
    }
    break;

                case 'varbinary':
                case 'mediumblob':
                case 'blob':
                case 'longblob':
                    // No special processing for MySQL is needed
                    break;
            }

            return $value;
        }
    }

Upgrade the Module : php bin/magento set:up

Flush Cache : php bin/magento flush:cache

  • There is nothing wrong with the Pdo this is bad practice and not a solution just a hack to get around the actual issue – Dava Gordon Sep 3 at 9:45
0

The reason behind this error you are passing the array to the column that is causing the issue. You can use the following code to avoid this error.

if (isset($data['logo'][0]['name']) && isset($data['logo'][0]['tmp_name'])) {
                $data['image'] =$data['logo'][0]['name'];
                $this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
                'QaisarSatti\HelloWorld\HelloWorldImageUpload'
            );
                $this->imageUploader->moveFileFromTmp($data['image']);
            } elseif (isset($data['logo'][0]['image']) && !isset($data['logo'][0]['tmp_name'])) {
                $data['image'] = $data['logo'][0]['image'];
            } else {
                $data['image'] = null;
            }

Reference

  • thanks for your answer. first time i can save the image, this is happening only on edit – Mujahidh Sep 3 at 9:30
  • @Mujahidh if your having issues editing you need to edit your data provider and convert it back into an array remember when saving you need to save both parameters – Dava Gordon Sep 3 at 9:43
  • @Mujahidh elseif (isset($data['logo'][0]['image']) && !isset($data['logo'][0]['tmp_name'])) { $data['image'] = $data['logo'][0]['image']; } code for edit – Qaisar Satti Sep 3 at 9:59

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.