Read More
Menu Close

    Fixed: missing required argument $model of Grid Collection class

    In Magento 2, sometimes we create a grid to display records at the frontend in our custom module. And when we create a grid, sometimes due to a minor mistake, when we will change deployment mode(in default or developer mode), then we can get an exception as follows:

    “Missing required argument $model of Vendor\CustomModule\Model\ResourceModel\Record\Grid\Collection.”

    Here, I have created a Grid Collection class as follows inside the app/code/Vendor/CustomModule/Model/ResourceModel/Record/Grid/ directory.

    <?php
    /**
     *
     * @category  Vendor
     * @package   Vendor_CustomModule
     * @author    Vendor
     * @copyright Copyright (c) Vendor (https://example.com)
     * @license   https://example.com/license.html
     */
    
    namespace Vendor\CustomModule\Model\ResourceModel\Record\Grid;
    
    use Magento\Framework\Api\Search\SearchResultInterface;
    use Magento\Framework\Search\AggregationInterface;
    use Vendor\CustomModule\Model\ResourceModel\Record\Collection as MessageCollection;
    
    class Collection extends MessageCollection implements SearchResultInterface
    {
        /**
         * @var AggregationInterface
         */
        protected $_aggregations;
    
        protected $eavAttribute;
    
        /**
         * @param \Magento\Framework\Data\Collection\EntityFactoryInterface    $entityFactory
         * @param \Psr\Log\LoggerInterface                                     $logger
         * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
         * @param \Magento\Framework\Event\ManagerInterface                    $eventManager
         * @param \Magento\Store\Model\StoreManagerInterface                   $storeManager
         * @param [type]                                                       $mainTable
         * @param [type]                                                       $eventPrefix
         * @param [type]                                                       $eventObject
         * @param [type]                                                       $resourceModel
         * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute            $eavAttribute
         * @param string                                                       $model
         * @param [type]                                                       $connection
         * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb         $resource
         */
        public function __construct(
            \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
            \Psr\Log\LoggerInterface $logger,
            \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
            \Magento\Framework\Event\ManagerInterface $eventManager,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            $mainTable,
            $eventPrefix,
            $eventObject,
            $resourceModel,
            \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute,
            $model = \Magento\Framework\View\Element\UiComponent\DataProvider\Document::class,
            \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
            \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
            \Magento\Customer\Model\Session $customerSession
        ) {
            $this->eavAttribute = $eavAttribute;
            parent::__construct(
                $entityFactory,
                $logger,
                $fetchStrategy,
                $eventManager,
                $storeManager,
                $connection,
                $resource
            );
            $this->_eventPrefix = $eventPrefix;
            $this->_eventObject = $eventObject;
            $this->_init($model, $resourceModel);
            $this->setMainTable($mainTable);
            $this->_customerSession = $customerSession;
        }
    
        /**
         * @return AggregationInterface
         */
        public function getAggregations()
        {
            return $this->_aggregations;
        }
    
        /**
         * @param AggregationInterface $aggregations
         * @return $this
         */
        public function setAggregations($aggregations)
        {
            $this->_aggregations = $aggregations;
        }
    
        /**
         * Retrieve clear select
         *
         * @return \Magento\Framework\DB\Select
         */
        protected function _getClearSelect()
        {
            return $this->_buildClearSelect();
        }
        /**
         * Build clear select
         *
         * @param  \Magento\Framework\DB\Select $select
         * @return \Magento\Framework\DB\Select
         */
        protected function _buildClearSelect($select = null)
        {
            if (null === $select) {
                $select = clone $this->getSelect();
            }
            $select->reset(\Magento\Framework\DB\Select::ORDER);
            $select->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
            $select->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
            $select->reset(\Magento\Framework\DB\Select::COLUMNS);
            return $select;
        }
         
        /**
         * Retrieve all ids for collection
         * Backward compatibility with EAV collection
         *
         * @param  int $limit
         * @param  int $offset
         * @return array
         */
        public function getAllIds($limit = null, $offset = null)
        {
            $idsSelect = $this->_getClearSelect();
            $idsSelect->columns('entity_id');
            $idsSelect->limit($limit, $offset);
            $idsSelect->resetJoinLeft();
            return $this->getConnection()->fetchCol($idsSelect, $this->_bindParams);
        }
    
        /**
         * Get search criteria.
         *
         * @return \Magento\Framework\Api\SearchCriteriaInterface|null
         */
        public function getSearchCriteria()
        {
            return null;
        }
    
        /**
         * Set search criteria.
         *
         * @param  \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
         * @return $this
         */
        public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
        {
            return $this;
        }
    
        /**
         * Get total count.
         *
         * @return int
         */
        public function getTotalCount()
        {
            return $this->getSize();
        }
    
        /**
         * Set total count.
         *
         * @param  int $totalCount
         * @return $this
         */
        public function setTotalCount($totalCount)
        {
            return $this;
        }
    
        /**
         * Set items list.
         *
         * @param  \Magento\Framework\Api\ExtensibleDataInterface[] $items
         * @return $this
         */
        public function setItems(array $items = null)
        {
            return $this;
        }
        /**
         * Join to get Product name in grid.
         */
        protected function _renderFiltersBefore()
        {
            $customerId = $this->_customerSession->getCustomer()->getId();
            $this->getSelect()->group("main_table.entity_id");
            $this->addFieldToFilter('customer_id', ['eq' => $customerId]);
            $this->addFieldToFilter('status', ['eq' => 1]);
            parent::_renderFiltersBefore();
        }
    }
    

    Now, when we will execute the controller at the frontend, we will get the record in the grid. But when we execute the following command to change the deployment mode, we can get an exception(please refer to the following image) on the grid collection class.

    bin/magento deploy:mode:set developer
    OR
    bin/magento deploy:mode:set default

    modelError
    missing $model argument exception

    Reason for the Exception: As you can see in the above Grid Collection class we have created a constructor, the exception is occurring due to the wrong sequence of passing the arguments. In the Magento coding standard, in the method definition, those arguments should be passed in last which have assigned a default value.

    Solution: We have to correct the sequence of arguments in the constructor as follows.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    /**
         * @param \Magento\Framework\Data\Collection\EntityFactoryInterface    $entityFactory
         * @param \Psr\Log\LoggerInterface                                     $logger
         * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
         * @param \Magento\Framework\Event\ManagerInterface                    $eventManager
         * @param \Magento\Store\Model\StoreManagerInterface                   $storeManager
         * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute            $eavAttribute
         * @param \Magento\Customer\Model\Session                              $customerSession
         * @param [type]                                                       $mainTable
         * @param [type]                                                       $eventPrefix
         * @param [type]                                                       $eventObject
         * @param [type]                                                       $resourceModel
         * @param string                                                       $model
         * @param [type]                                                       $connection
         * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb         $resource
         */
        public function __construct(
            \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
            \Psr\Log\LoggerInterface $logger,
            \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
            \Magento\Framework\Event\ManagerInterface $eventManager,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute,
            \Magento\Customer\Model\Session $customerSession,
            $mainTable,
            $eventPrefix,
            $eventObject,
            $resourceModel,
            $model = \Magento\Framework\View\Element\UiComponent\DataProvider\Document::class,
            \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
            \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
        ) {
            $this->eavAttribute = $eavAttribute;
            parent::__construct(
                $entityFactory,
                $logger,
                $fetchStrategy,
                $eventManager,
                $storeManager,
                $connection,
                $resource
            );
            $this->_eventPrefix = $eventPrefix;
            $this->_eventObject = $eventObject;
            $this->_init($model, $resourceModel);
            $this->setMainTable($mainTable);
            $this->_customerSession = $customerSession;
        }

    Now, we have to run the following commands:

    rm -rf generated/
    php bin/magento cache:flush


    Now, the issue will be fixed. And see the result in the following image:

    modelFix
    missing $model argument exception fixed

    Hope this will be helpful.

    Thanks 🙂

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top