I am recently working with Magento and am trying to display a set of data from a view in database. I want to filter it based on Agent Id! I am trying to filter the data in

Vendor\Module\Ui\Component\Listing\DataProviders\Atisstats\Coupons\Listing class like this:

namespace Atis\Stats\Ui\Component\Listing\DataProviders\Atisstats\Coupons;

use \Atis\Stats\Model\ResourceModel\Coupons\CollectionFactory; use \Magento\Framework\Registry; use \Magento\Ui\DataProvider\AbstractDataProvider;

class Listing extends AbstractDataProvider {

protected $_registry;

public function __construct(
    Registry $registry,
    CollectionFactory $collectionFactory,
    array $meta = [],
    array $data = []
) {
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    $this->collection = $collectionFactory->create();
    $this->_registry = $registry;

    // get id-s of all dealers

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('\Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $select = $connection->select()
    $data = $connection->fetchAll($select);

    // compare this array of id-s it with the value in session. If they are equal this means that a dealer is logged in so i will display only his statistics

    $givenAgentId = 0;
    foreach ($data as $dealerId) {
        if ($dealerId['user_id'] == $this->_registry->registry('admin_session_id')) {
            $givenAgentId = $this->_registry->registry('admin_session_id');

    // if the value of $givenAgentId is not 0 this means that a dealer is logged in so i can filter data based on his id
    // when I replace the $givenAgentId variable with a number, lets say 4, it works ok, but otherwise it wont filter anything

    if ($givenAgentId != 0) {
        $this->collection->addFieldToFilter('agent_id', $givenAgentId);


I don't know why $this->collection->addFieldToFilter('agent_id', $givenAgentId); it wont work. I tried to convert $givenAgentId into string and int but still nothing!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.