0

I'm trying to add a custom attribute to the customer_entity table and then sort the collection with that custom attribute.

So far I've tried using etc/db_schema.xml to add a column to the table, and even though the column got added to the table, $collection->addAttributeToFilter('my_custom_attribute', '1') returned 'attribute 'my_custom_attribute' is invalid'.

I then tried to create a data patch, following various example online, to add an eav attribute. Here's my patch :

<?php

use Magento\Customer\Model\Customer;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchInterface;

class AddMagasinAttributeToCustomer 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;
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }

    public function apply()
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $eavSetup->addAttribute(Customer::ENTITY, 'magasin', [
            'type' => 'int',
        ]);

    }
}

Now the eav attribute is present in the customer_eav_attribute table. I updated some customer data with the custom attribute value set to 1, but now, $collection->addAttributeToFilter('my_custom_attribute', '1') returns an empty array.

I have dumped the value of the corresponding record without any filter and the custom attribute value is set in the table.

What am I doing wrong here ?

0

Turns out $collection->addFilter('my_custom_attribute', my_custom_value) returns the correct filtered data. I still don't know why the other method does not work though.

| improve this answer | |

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.