0

I have created a custom grid, in this grid collection I have to inject helper .while injecting helper in the collection it throws error like this Type Error occurred when creating object:

My collection:

namespace vendor\module\Model\ResourceModel\Post;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

    class Collection extends AbstractCollection
    {
        /**
         * Define model & resource model
         */
        const YOUR_TABLE = 'xxx';


        protected $helper;
        public function __construct(
            \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
            \Psr\Log\LoggerInterface $logger,
            \vendor\magento\Helper\Userdata $helper,
            \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
            \Magento\Framework\Event\ManagerInterface $eventManager,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
            \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
        ) {
            $this->_init(
                ' vendor\module\Model\Post', ' vendor\module\Model\ResourceModel\Post'
            );
            parent::__construct(
                $entityFactory, $logger, $fetchStrategy, $eventManager, $connection,$resource,$helper
            );
            $this->storeManager = $storeManager;

        }
        protected function _initSelect()
        {
            parent::_initSelect();

            $this->getSelect()->where("main_table.user_id = 2";
        }
8
  • Will you please share the code? I think it would be helpful. Commented Feb 23, 2020 at 4:13
  • @Mathanagopal shared the code Commented Feb 23, 2020 at 4:42
  • 1. I am not seeing the helper method being injected in the constructor. 2. If you want to use the helper method, pass it as a parameter to the constructor and pass it onto the parent::__constructor. 3. Also, helpers are best helpful to be used in the controller or block class in Magento. Commented Feb 23, 2020 at 4:56
  • I have updated the code with helper ..still error display Commented Feb 23, 2020 at 5:09
  • The interceptor file needs to be re-created. So kindly run di:compile and clean cache. Hope this helps Commented Feb 23, 2020 at 5:17

2 Answers 2

0
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,
    \Vendor\Module\Helper\YourHelperClass $helper,  
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
    \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
    $this->helper = $helper;
    parent::__construct(
        $entityFactory, $logger, $fetchStrategy, $eventManager, $connection,
        $resource
    );
}

protected function _construct()
{
    $this->_init(
        'Vendor\Module\Model\Post', ' vendor\module\Model\ResourceModel\Post'
    );
}
2
  • Thanks for your reply ..it was worked for me thanks alot Commented Feb 24, 2020 at 10:05
  • It's my pleasure. Commented Feb 24, 2020 at 11:34
0

Inject your helper before below line

\Magento\Framework\DB\Adapter\AdapterInterface $connection = null as below

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,
    \NameSpace\Module\Helper\YourHelperClass  
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
    \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
    $this->_init(
        ' vendor\module\Model\Post', ' vendor\module\Model\ResourceModel\Post'
    );
    parent::__construct(
        $entityFactory, $logger, $fetchStrategy, $eventManager, $connection,
        $resource
    );
    $this->storeManager = $storeManager;

}
2
  • still it display Type Error occurred when creating object Commented Feb 23, 2020 at 5:15
  • Try giving the helper method after $resource in the constructor. Refer here for the order in which the constructor expects the parameters. Hope this works. Commented Feb 23, 2020 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.