在模块开发期间,我一直在编写一个插件,该插件在加载产品选项集合之前会对其进行修改(添加描述字段)。就这个:
等/ di.xml
<type name="Magento\Catalog\Model\ResourceModel\Product\Option\Collection">
<plugin name="addOptionDescription" type="Vendor\Module\Plugin\Product\Option\Collection" sortOrder="10" disabled="false"/>
</type>
码:
<?php
namespace Vendor\Module\Plugin\Product\Option;
use Vendor\Module\Model\OptionDescription;
use Magento\Catalog\Model\ResourceModel\Product\Option\Collection as OptionCollection;
class Collection
{
/**
* @var \Vendor\Module\Helper\Data
*/
protected $helper;
public function __construct(
\Vendor\Module\Helper\Data $helper
) {
$this->helper = $helper;
}
/**
* @param OptionCollection $subject
* @param bool $printQuery
* @param bool $logQuery
* @return array
*/
public function beforeLoad($subject, $printQuery = false, $logQuery = false)
{
if (!$subject->isLoaded()) {
$this->addDescriptionToResult($subject);
}
return [$printQuery, $logQuery];
}
/**
* Add description to result
*
* If yo get error message "Warning: Illegal string offset 'is_in_stock' ... "
* @see http://devdocs.magento.com/guides/v2.0/install-gde/trouble/php/tshoot_opcache.html
*
* @param OptionCollection $collection
* @return OptionCollection $collection
*/
private function addDescriptionToResult($collection)
{
$tableName = OptionDescription::TABLE_NAME;
$joinDescriptionExpr = 'main_table.unique_option_id = option_description.unique_option_id AND option_description.store_id = 0';
$collection->getSelect()->joinLeft(
['option_description' => $tableName],
$joinDescriptionExpr,
['description' => 'description']
);
return $collection;
}
/**
* Resolve current store id
*
* @return int
*/
protected function getStoreId()
{
return $this->helper->resolveCurrentStoreId();
}
}
一切似乎都还可以,但是...当我尝试加载现有产品编辑页面(在后端)时,我看到以下错误:
警告:在第87行上的/vendor/magento/module-catalog-inventory/Ui/DataProvider/Product/Form/Modifier/AdvancedInventory.php中,非法字符串偏移量'is_in_stock'
万一我按照文档中的指示进行更改(opcache.save_comments = 1
在我的php-fpm配置中设置),一切正常。但是我不明白,是哪个代码导致了上面的错误,以及如何在不修改配置的情况下防止它发生?