2

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

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, privacy policy and cookie policy

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