解决非法偏移错误

本文提供了一个解决方案,用于在Adobe Commerce 2.1或更高版本中,当您在Commerce管理员中创建新产品时收到解决非法偏移错误。

在Adobe Commerce 2.1或更高版本中,在Commerce管理员中创建新产品时,可能会显示以下错误:

Warning: Illegal string offset 'is_in_stock' in [...]/vendor/
magento/module-catalog-inventory/Ui/DataProvider/Product/Form/
Modifier/AdvancedInventory.php on line 87

详细信息

Adobe Commerce 2.1及更高版本在Magento\Framework\Api\ExtensionAttributesFactory.php中的getExtensionAttributes方法的getDocComment验证调用中使用PHP代码注释。

如果您启用了PHP OPcache(我们建议),则会显示此错误,因为默认情况下,OPcache设置opcache.save_comments处于禁用状态。

解决方法

要解决此问题,请找到您的OPcache配置设置并按如下方式启用opcache.save_comments

步骤1:找到OPcache配置

要查找OPcache配置设置,请执行以下操作:

PHP OPcache设置通常位于php.iniopcache.ini中。 该位置可能取决于您的操作系统和PHP版本。 OPcache配置文件可能具有[opcache]部分或类似于opcache.enable的设置。

请使用以下指南查找它:

  • Apache Web Server:

对于带有Apache的Ubuntu,OPcache设置通常位于php.ini中。

对于具有Apache或nginx的CentOS,OPcache设置通常位于/etc/php.d/opcache.ini中。

如果没有,请使用以下命令找到它:

    $ sudo find / -name 'opcache.ini'
  • PHP-FPM的nginx Web服务器: /etc/php5/fpm/php.ini

如果您有多个opcache.ini,请修改全部。

步骤2:启用opcache.save_comments

  1. 在文本编辑器中打开OPcache配置文件。

  2. 找到opcache.save_comments并取消评论(如有必要)。

  3. 确保其值设置为1

  4. 保存更改并退出文本编辑器。

  5. 重新启动Web服务器:

    • Apache, Ubuntu: service apache2 restart
    • Apache, CentOS: service httpd restart
    • nginx、Ubuntu和CentOS: service nginx restart
  6. 重新生成DI配置和所有可自动生成的缺失类:

    $ bin/magento setup:di:compile`
recommendation-more-help
8bd06ef0-b3d5-4137-b74e-d7b00485808a