0

I add new column to catalog_product_entity. so when i want to add new filter to collection i get this error The "user_id" attribute name is invalid. Reset the name and try again.

$subject->getCollection()->addFieldToFilter('user_id', array('eq' => '2'))
  • There may be two issues casuing this problem: You didn't add the column as eav attribute with backend_type = static (missing entry in eav_attribute table for attribute user_id) or the old DDL whithout your new column is cached (in this case try to flush the ddl cache: bin/magento cache:flush db_ddl) – HelgeB Apr 14 at 9:33
  • @HelgeB how i can add column as static attribute ? can you give me more detail . for secund issue i clear cache and update and compile – gh darvishani Apr 14 at 9:57
1

If you have added the column user_id manually to the table catalog_product_entity you can use the following way to filter your product collection:

$subject->getCollection()->getSelect()->where('user_id = 2');

or

$subject->getCollection()->getSelect()->where('e.user_id = 2'); 
//e is the reference for the table catalog_product_entity in the default product collection

That should work, beacuse this creates the SQL statement whithout checking if an attribute exists like the method addFieldToFilter does.

But it's not a good practice to alter core entity tables and add columns that way.

I would rather suggest to create a product attribute user_id of type integer and then your code with addFieldToFilter should work, without adding a column to catalog_product_entity. If you really want / need the column in catalog_product_entity for whatever reason you should at least create a product attribute user_id of type static and then you can use your code with addFieldToFilter too.

An example how to create product attributes programmatically can be found here How to create Catalog attribute in Magento 2 Using Upgrade Script

You have to set the correct type for your attribute ('type' => 'int' for an integer attribute or 'type' => 'static' if you want to use your added column)

There are also a lot of other examples for attribute creation if you google for it.

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.