Magento 2.3 - 无法通过数据/架构补丁(声明性架构)更改/插入表

我正在使用本指南和指南2作为参考,将自定义属性添加到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:

模块“CustomModle”:警告:call_user_func() 期望参数 1 是有效的回调,在 magento-root-folder/vendor/magento/framework/Setup/Patch/ 中找不到类

我设法创建了一个表(db_schema.xml 有效)但它是空的。出于某种原因,数据/架构补丁对我不起作用。模块路径是否正确(应用程序/代码/命名空间/模块)?什么可能导致这种行为?任何帮助,将不胜感激。


aluckdog
浏览 172回答 1
1回答

动漫人物

好像你的属性参数没有完成。尝试改用此代码。<?phpnamespace 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{&nbsp; &nbsp; /** @var ModuleDataSetupInterface */&nbsp; &nbsp; private $moduleDataSetup;&nbsp; &nbsp; /** @var EavSetupFactory */&nbsp; &nbsp; private $eavSetupFactory;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param ModuleDataSetupInterface $moduleDataSetup&nbsp; &nbsp; &nbsp;* @param EavSetupFactory $eavSetupFactory&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct(&nbsp; &nbsp; &nbsp; &nbsp; ModuleDataSetupInterface $moduleDataSetup,&nbsp; &nbsp; &nbsp; &nbsp; EavSetupFactory $eavSetupFactory&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; $this->moduleDataSetup = $moduleDataSetup;&nbsp; &nbsp; &nbsp; &nbsp; $this->eavSetupFactory = $eavSetupFactory;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* {@inheritdoc}&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function apply()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /** @var EavSetup $eavSetup */&nbsp; &nbsp; &nbsp; &nbsp; $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);&nbsp; &nbsp; &nbsp; &nbsp; $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'shop', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type' => 'int',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'backend' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'frontend' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => 'Shop',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'input' => 'select',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'visible' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'required' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user_defined' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'default' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'searchable' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filterable' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'comparable' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'visible_on_front' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'used_in_product_listing' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'unique' => false,&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; $groupName = 'Autosettings';&nbsp; &nbsp; &nbsp; &nbsp; $entityTypeId = $catalogSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);&nbsp; &nbsp; &nbsp; &nbsp; $attributeSetId = $catalogSetup->getAttributeSetId($entityTypeId, 'Default');&nbsp; &nbsp; &nbsp; &nbsp; $attribute = $catalogSetup->getAttribute($entityTypeId, 'shop');&nbsp; &nbsp; &nbsp; &nbsp; if ($attribute) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $catalogSetup->addAttributeToGroup(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $entityTypeId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attributeSetId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $groupName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attribute['attribute_id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 60&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* {@inheritdoc}&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static function getDependencies()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [];&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* {@inheritdoc}&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getAliases()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return [];&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP