Fix Error: Notice: Array to string conversion
Problem
The site is using the Amasty Product Feed extension. If you set the Schedule and trying to save feed profiles:
Unable to save feed with ID xx. Error: Notice: Array to string conversion in /magento_dir/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3105
My environment:
- PHP 7.2
- Magento 2.3.3
- Amasty Product Feed 2.5.1
Temporary solution
Although I updated the extension to the latest version, the error is still there. After debugged the extension, I figure out that it’s trying to store cron_day
& cron_time
values into DB with longtext
type.
I have to modify app/code/Amasty/Feed/Controller/Adminhtml/Feed/Save.php
, this is a temporary solution because the modified part may be overwritten by a new update. But it works.
The point is to remove unnecessary data when it saving the Feed Model. I added this function:
/** * Get schedule data & remove them from data * to preven Array to String conversion error * * @param $data * @return array */ private function getScheduleData(&$data) { $scheduleData = $data; if (isset($data[ScheduleInterface::CRON_DAY])) { unset($data[ScheduleInterface::CRON_DAY]); } if (isset($data[ScheduleInterface::CRON_TIME])) { unset($data[ScheduleInterface::CRON_TIME]); } return $scheduleData; }
This is how I use the function above:
$scheduleData = $this->getScheduleData($data); $model->setData($data); $this->_session->setPageData($model->getData()); $this->feedRepository->save($model, true); $this->scheduleManagement->saveScheduleData($model->getEntityId(), $scheduleData);
The Gist of the modified file: app/code/Amasty/Feed/Controller/Adminhtml/Feed/Save.php.