cancel
Showing results for 
Search instead for 
Did you mean: 

Bug? - addFieldToFilter function not working with URL variable

Bug? - addFieldToFilter function not working with URL variable

Having a strange issue trying to use a URL parameter to filter database rows for a grid on an admin page. I'm trying to configure the page so that it only returns rows with the order_num value specified by the order_num URL parameter. I have a custom class that extends the SearchResult class in order to accomplish this.

 

<?php

class OrderItems extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
    
    protected function _initSelect()
    {   
        $this->filterByOrderNum();
        
        parent::_initSelect();

        return $this;
    }

    private function filterByOrderNum() 
    { 
        $request = \Magento\Framework\App\ObjectManager::getInstance()
                       ->get('Magento\Framework\App\Request\Http'); 
       
        $ordNum = (int) $request->getParam('order_num');
       
        $this->addFieldToFilter('order_num', ['eq' => $ordNum]); 
       
        return $this; 
    }
}

 

Problem is, passing $ordNum doesn't retrieve any rows. If I replace $ordNum with a hardcoded number like so:

 

$this->addFieldToFilter('order_num', ['eq' => 10001]);

 

Then the correct rows for that order number are retrieved. I know this sounds like getParam is retrieving an incorrect value; however I've verified over and over again with var_dump that $request->getParam('order_num') is successfully retrieving whatever number I provide it (10001, for example). I've also run the following:

if ($ordNum === 10001){
    echo "Matched"; exit;
}

 Which successfully matched and triggered the exit, which should mean it's not an issue of type. At this point I'm thinking it's either a character encoding issue that's going over my head or I've stumbled onto a bug in the addFieldToFilter function. Any thoughts would be greatly appreciated.

 

UPDATE: One other weird thing of note; I started playing around with inequalities, and for some reason 'gt' will return all database rows while 'lt' will return none. Seemed worth sharing.