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.

 

3 REPLIES 3

Re: Bug? - addFieldToFilter function not working with URL variable

Did You Find Any Solution ??

Re: Bug? - addFieldToFilter function not working with URL variable

We have exactly the same issue... We have tried everything with the value itself. We have put it as an int within as a key value in an array and flipped the array and extracted it just to have no link with the param at all. Not that it would have that but just in case. nothing helped

Re: Bug? - addFieldToFilter function not working with URL variable

We have solved this issue by doing the following :

    /**
     * CcCustompriceProductListingDataProvider constructor.
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param \Magento\Framework\Api\Search\ReportingInterface $reporting
     * @param \Magento\Framework\Api\Search\SearchCriteriaBuilder $searchCriteriaBuilder
     * @param \Magento\Framework\App\RequestInterface $request
     * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
     * @param array $meta
     * @param array $data
     * @throws \Exception
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        ReportingInterface $reporting,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        RequestInterface $request,
        FilterBuilder $filterBuilder,
        array $meta = [],
        array $data = []
    ) {
        $data['config']['filter_url_params']['product_id'] = $request->getParam('cppc_product_id', 0);
        parent::__construct($name, $primaryFieldName, $requestFieldName, $reporting, $searchCriteriaBuilder, $request, $filterBuilder, $meta, $data);
    }

You do not need to use any other function. The reason why this is is because it is also updated with an update URL and that does not have that parameter. By using adding that to the data it also parses that into the update url.


You can see that here (Parent function)

    /**
     * @return void
     */
    protected function prepareUpdateUrl()
    {
        if (!isset($this->data['config']['filter_url_params'])) {
            return;
        }
        foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
            if ('*' == $paramValue) {
                $paramValue = $this->request->getParam($paramName);
            }
            if ($paramValue) {
                $this->data['config']['update_url'] = sprintf(
                    '%s%s/%s/',
                    $this->data['config']['update_url'],
                    $paramName,
                    $paramValue
                );
                $this->addFilter(
                    $this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create()
                );
            }
        }
    }