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 MagentoCustomerModelCustomer;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchInterface;
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 ?