5

i'm using magento 2 path system but it's not working, when i do a setup upgrade it doesn't reach my die, could you explain me why ?

<?php

namespace MyVendor\Company\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

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

    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Constructor.
     *
     * @param ModuleDataSetupInterface $moduleDataSetup 
     * @param CustomerSetupFactory     $customerSetupFactory 
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        die('potato');
        $this->moduleDataSetup->getConnection()->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->addAttribute(
            Customer::ENTITY,
            'company',
            [
                'type' => 'int',
                'label' => 'Company',
                'input' => 'select',
                'required' => true,
                'position' => 30,
                'system' => false,
                'source_model' => \MyVendor\Company\Model\Customer\Attribute\Source\Company::class
            ]
        );

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'company')
            ->addData(
                [
                    'used_in_forms' => [
                        'adminhtml_customer'
                    ]
                ]
            );
        $attribute->save();

        $this->moduleDataSetup->getConnection()->endSetup();
    }

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

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

3 Answers 3

15

If you have already ran setup_upgrade before, there is a table in the database called patch_list that will be filled because patches are only executed once. You could go in the database and delete the entry and then run setup_upgrade again.

1
  • 1
    This is the main point to be noted.
    – MagePsycho
    Aug 29, 2022 at 20:00
3

Include the below function and the given version should be greater than the version specified in module.xml

public static function getVersion()
{
  return '2.3.6';
}
2
1

I found out why. For peoples who has the same issue, check first you don't have created an installData like the old way to intall/upgrade your data.

If you want to migrate without duplicate your data, you should create a patch file for each install / upgrade data file you have and you could use the \Magento\Framework\Setup\Patch\PatchVersionInterface to define a specific version.

See https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/data-patches.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.