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.
eav_attribute
table for attributeuser_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
)