cancel
Showing results for 
Search instead for 
Did you mean: 

Error while using data patch in custom module (declarative schema)

Error while using data patch in custom module (declarative schema)

I'm trying to add a custom attribute to 'catalog_product' but patch file isn't working as expected. Here is DataPatch php file (AddShopAttribute.php):

<?php
namespace Namespace\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 [];
    }
}

I'm getting this error when running command `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 /var/www/mage/html/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;
    }

Ive managed to create a table too (db_module.xml works) but is empty. For some reason Patch files isn't working for me. What could be causing this behavior? Any help would be appreciated.

1 REPLY 1

Re: Error while using data patch in custom module (declarative schema)

Hi,

 

Please check your namespace.

It is having following value

namespace Namespace\CustomModule\Patch\Data;

Whereas it should have 'Setup' directory name too. Like below.

namespace Namespace\CustomModule\Setup\Patch\Data;

As of now, this is the only thing i found incorrect. Check if that helps.