Magento 2.3 - 无法通过 data/schema 补丁(声明式模式)alter/insert 进入表格

Magento 2.3 - Can't alter/insert into tables via data/schema patch (declarative schema)

我正在使用此 guide and 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:

时收到此错误

Module 'CustomModule': Warning: call_user_func() expects parameter 1 to be a valid callback, class 'CustomModule\Setup\Patch\Data\AddShopAttribute' not found in magento-root-folder/vendor/magento/framework/Setup/Patch/PatchRegistry.php on line 139

这里是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;
        }

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

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

<?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 [];
    }
}