0

我正在使用本指南guide2作为向 中添加自定义属性的参考catalog_product,但补丁不起作用。

这是补丁文件(JKM\CustomModule\Setup\Patch\Data\AddShopAttribute.php):

<?php
namespace JKM\CustomModule\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddShopAttribute implements DataPatchInterface
{
    /** @var ModuleDataSetupInterface */
    private $moduleDataSetup;

    /** @var EavSetupFactory */
    private $eavSetupFactory;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $eavSetup->addAttribute('catalog_product', 'shop', [
            'type' => 'int',
            'label' => 'Shop',
            'input' => 'select',
            'used_in_product_listing' => true,
            'user_defined' => true,
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

运行命令时出现此错误bin/magento setup:upgrade

模块“CustomModule”:警告:call_user_func() 期望参数 1 是有效回调,在 magento-root-folder/vendor/magento/framework/Setup/Patch/ 中找不到类“CustomModule\Setup\Patch\Data\AddShopAttribute”第 139 行的 PatchRegistry.php

这是 PatchRegistry.php

 private function getDependencies(string $patch)
        {
            $depInstances = [];
            $deps = call_user_func([$patch, 'getDependencies']); // LINE 139
            $this->cyclomaticStack[$patch] = true;

            foreach ($deps as $dep) {
                if (isset($this->cyclomaticStack[$dep])) {
                    throw new \LogicException("Cyclomatic dependency during patch installation");
                }

                $depInstance = $this->registerPatch($dep);
                /**
                 * If a patch already have applied dependency - than we definently know
                 * that all other dependencies in dependency chain are applied too, so we can skip this dep
                 */
                if (!$depInstance) {
                    continue;
                }

                $depInstances = array_replace($depInstances, $this->getDependencies($this->patches[$dep]));
                $depInstances[$depInstance] = $depInstance;
            }

            unset($this->cyclomaticStack[$patch]);
            return $depInstances;
        }

我设法创建了一个表(db_schema.xml 有效),但它是空的。出于某种原因,Data/Schema Patch 对我不起作用。模块路径是否正确(app/code/namespace/module)?什么可能导致这种行为?任何帮助,将不胜感激。

4

1 回答 1

1

您的属性参数似乎没有完成。尝试改用此代码。

<?php
namespace JKM\CustomModule\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddShopAttribute implements DataPatchInterface
{
    /** @var ModuleDataSetupInterface */
    private $moduleDataSetup;

    /** @var EavSetupFactory */
    private $eavSetupFactory;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'shop', [
            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'label' => 'Shop',
            'input' => 'select',
            'class' => '',
            'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
        ]);

        $groupName = 'Autosettings';
        $entityTypeId = $catalogSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
        $attributeSetId = $catalogSetup->getAttributeSetId($entityTypeId, 'Default');
        $attribute = $catalogSetup->getAttribute($entityTypeId, 'shop');
        if ($attribute) {
            $catalogSetup->addAttributeToGroup(
                $entityTypeId,
                $attributeSetId,
                $groupName,
                $attribute['attribute_id'],
                60
            );
        }
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}
于 2019-09-19T19:22:25.320 回答