I’m trying to customize the query being executed by magento 2 when filtering in the Admin grid. Assuming I have a simple table and the query executed is
select * from main_table
Now when I try to apply filter magento runs something like this
select * from main_table where main_table.created_at = 'the_date_selected'
but I want to modify this behavior. Instead of running it like above I want to output something like this
select * from main_table where main_table.created_at in (select create_at from table_2 where created_at = 'the_date_selected')
Now I don’t know how to achieve this. I created a _initselect to modify the first load of the UI Grid result. But the problem is when I try to filter it. I have a code something like below
protected function _initSelect()
{
parent::_initSelect();
$this->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns([
'entity_id',
'name',
'sku'
])
->where('entity_id NOT IN (select product_id from sales_order_item)');
}
As you can see I want to insert the value of the filters inside the subquery select product_id from sales_order_item
instead from the main query.
How do I do this in Magento 2?