1

I had an error like below,

 Fatal error: Uncaught TypeError: Argument 5 passed to
 Webkul\Pos\Model\ResourceModel\Agent\Collection::__construct() must be
 an instance of Magento\Framework\App\ResourceConnection, null given,
 called in

/var/www/sqdelivery/generated/code/Webkul/Pos/Model/ResourceModel/Agent/Collection/Interceptor.php on line 14 and defined in

/var/www/sqdelivery/app/code/Webkul/Pos/Model/ResourceModel/Agent/Collection.php:31 Stack trace: #0

/var/www/sqdelivery/generated/code/Webkul/Pos/Model/ResourceModel/Agent/Collection/Interceptor.php(14):

 Webkul\Pos\Model\ResourceModel\Agent\Collection->__construct(Object(Magento\Framework\Data\Collection\EntityFactory), Object(Magento\Framework\Logger\Monolog),
 Object(Magento\Framework\Data\Collection\Db\FetchStrategy\Query),
 Object(Magento\Framework\Event\Manager\Proxy), NULL, NULL) #1

/var/www/sqdelivery/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(111):

 Webkul\Pos\Model\ResourceModel\Agent\Collection\Interceptor->__construct(Object(Magento\Framework\Data\Collection\EntityFac
 in
 **/var/www/sqdelivery/app/code/Webkul/Pos/Model/ResourceModel/Agent/Collection.php
 on line 31**

what is does means.?? My Collection file code is below,

<?php

namespace Webkul\Pos\Model\ResourceModel\Agent;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection  {

     const AGENT_MODEL_NAME = Webkul\Pos\Model\Agent::class;

/**
 * @var \Magento\Framework\DataObject\Copy\Config
 */
protected $_fieldsetConfig;

/**
 * @var string
 */
protected $_modelName;

     public function __construct(
    \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
    \Magento\Framework\Event\ManagerInterface $eventManager,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\Framework\Validator\UniversalFactory $universalFactory,
    \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
    \Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
    $modelName = self::AGENT_MODEL_NAME
) {
    $this->_fieldsetConfig = $fieldsetConfig;
    $this->_modelName = $modelName;
    parent::__construct(
        $entityFactory,
        $logger,
        $fetchStrategy,
        $eventManager,
        $eavConfig,
        $resource,
        $eavEntityFactory,
        $resourceHelper,
        $universalFactory,
        $entitySnapshot,
        $connection
    );
}

    protected $_idFieldName = "id";



    protected function _construct()  {
        $this->_init("Webkul\Pos\Model\Agent", "Webkul\Pos\Model\ResourceModel\Agent");
        $this->_map["fields"]["id"] = "main_table.id";
    }

    public function setAgentData($condition, $attributeData)  {
        return $this->getConnection()->update($this->getTable("pos_agent"), $attributeData, $where = $condition);
    }

    public function addNameToSelect()
   {
    $fields = [];

    $agentAccount = $this->_fieldsetConfig->getFieldset('agent_account');
     print_r($agentAccount);
     exit;

    foreach ($agentAccount as $code => $field) {
        if (isset($field['email'])) {
            $fields[$code] = $code;
        }
    }

    $connection = $this->getConnection();
    $concatenate = [];
    if (isset($fields['prefix'])) {
        $concatenate[] = $connection->getCheckSql(
            '{{prefix}} IS NOT NULL AND {{prefix}} != \'\'',
            $connection->getConcatSql(['LTRIM(RTRIM({{prefix}}))', '\' \'']),
            '\'\''
        );
    }
    $concatenate[] = 'LTRIM(RTRIM({{firstname}}))';
    $concatenate[] = '\' \'';
    if (isset($fields['middlename'])) {
        $concatenate[] = $connection->getCheckSql(
            '{{middlename}} IS NOT NULL AND {{middlename}} != \'\'',
            $connection->getConcatSql(['LTRIM(RTRIM({{middlename}}))', '\' \'']),
            '\'\''
        );
    }
    $concatenate[] = 'LTRIM(RTRIM({{lastname}}))';
    if (isset($fields['suffix'])) {
        $concatenate[] = $connection->getCheckSql(
            '{{suffix}} IS NOT NULL AND {{suffix}} != \'\'',
            $connection->getConcatSql(['\' \'', 'LTRIM(RTRIM({{suffix}}))']),
            '\'\''
        );
    }

    $nameExpr = $connection->getConcatSql($concatenate);

    $this->addExpressionAttributeToSelect('name', $nameExpr, $fields);

    return $this;
    }

public function getSelectCountSql()
{
    $select = parent::getSelectCountSql();
    $select->resetJoinLeft();

    return $select;
}

/**
 * Reset left join
 *
 * @param int $limit
 * @param int $offset
 * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
 */
protected function _getAllIdsSelect($limit = null, $offset = null)
{
    $idsSelect = parent::_getAllIdsSelect($limit, $offset);
    $idsSelect->resetJoinLeft();
    return $idsSelect;
}

}

Anyone please help me to slove this error. Thanks in advance.

1
  • don't forget to execute bin/magento setup:upgrade after finished edit your construct class
    – mrfizh
    Commented Sep 24, 2018 at 10:40

3 Answers 3

0

Your construct should be

public function __construct(
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\App\ResourceConnection $resource,
\Magento\Framework\Validator\UniversalFactory $universalFactory,
\Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
\Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
$modelName = self::AGENT_MODEL_NAME
 ) {
$this->_fieldsetConfig = $fieldsetConfig;
$this->_modelName = $modelName;
parent::__construct(
    $entityFactory,
    $logger,
    $fetchStrategy,
    $eventManager,
    $resource,
    $universalFactory,
    $entitySnapshot,
    $connection
);

}

0

You need to replace the __contruct like below:

public function __construct(
    \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
    \Magento\Framework\Event\ManagerInterface $eventManager,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\Framework\Validator\UniversalFactory $universalFactory,
    \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
    \Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
    $modelName = self::AGENT_MODEL_NAME
) {
    $this->_fieldsetConfig = $fieldsetConfig;
    $this->_modelName = $modelName;
    parent::__construct(
        $entityFactory,
        $logger,
        $fetchStrategy,
        $eventManager,
        $connection,
        $resource
    );
}

Because if you see the parent class AbstractCollection , it has only 6 parameters(see 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\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        $this->_eventManager = $eventManager;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $connection);
        $this->_construct();
        $this->_resource = $resource;
        $this->setConnection($this->getResource()->getConnection());
        $this->_initSelect();
    }
0

Try this code:

<?php

namespace Webkul\Pos\Model\ResourceModel\Agent;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection  {

    const AGENT_MODEL_NAME = Webkul\Pos\Model\Agent::class;

    /**
     * @var \Magento\Framework\DataObject\Copy\Config
     */
    protected $_fieldsetConfig;

    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\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
        \Magento\Framework\App\ResourceConnection $resourceConnection,
        \Magento\Framework\DataObject\Copy\Config $fieldsetConfig
    ) {
        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $connection
            $resource
        );
        $this->_fieldsetConfig = $fieldsetConfig;
        $this->resourceConnection = $resourceConnection;
        $this->setConnection($this->resourceConnection->getConnection());
    }

    protected $_idFieldName = "id";



    protected function _construct()  {
        $this->_init("Webkul\Pos\Model\Agent", "Webkul\Pos\Model\ResourceModel\Agent");
        $this->_map["fields"]["id"] = "main_table.id";
    }

    public function setAgentData($condition, $attributeData)  {
        return $this->getConnection()->update($this->getTable("pos_agent"), $attributeData, $where = $condition);
    }

   public function addNameToSelect()
   {
        $fields = [];

        $agentAccount = $this->_fieldsetConfig->getFieldset('agent_account');
         print_r($agentAccount);
         exit;

        foreach ($agentAccount as $code => $field) {
            if (isset($field['email'])) {
                $fields[$code] = $code;
            }
        }

        $connection = $this->getConnection();
        $concatenate = [];
        if (isset($fields['prefix'])) {
            $concatenate[] = $connection->getCheckSql(
                '{{prefix}} IS NOT NULL AND {{prefix}} != \'\'',
                $connection->getConcatSql(['LTRIM(RTRIM({{prefix}}))', '\' \'']),
                '\'\''
            );
        }
        $concatenate[] = 'LTRIM(RTRIM({{firstname}}))';
        $concatenate[] = '\' \'';
        if (isset($fields['middlename'])) {
            $concatenate[] = $connection->getCheckSql(
                '{{middlename}} IS NOT NULL AND {{middlename}} != \'\'',
                $connection->getConcatSql(['LTRIM(RTRIM({{middlename}}))', '\' \'']),
                '\'\''
            );
        }
        $concatenate[] = 'LTRIM(RTRIM({{lastname}}))';
        if (isset($fields['suffix'])) {
            $concatenate[] = $connection->getCheckSql(
                '{{suffix}} IS NOT NULL AND {{suffix}} != \'\'',
                $connection->getConcatSql(['\' \'', 'LTRIM(RTRIM({{suffix}}))']),
                '\'\''
            );
        }

        $nameExpr = $connection->getConcatSql($concatenate);

        $this->addExpressionAttributeToSelect('name', $nameExpr, $fields);

        return $this;
    }

    public function getSelectCountSql()
    {
        $select = parent::getSelectCountSql();
        $select->resetJoinLeft();

        return $select;
    }

    /**
     * Reset left join
     *
     * @param int $limit
     * @param int $offset
     * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
     */
    protected function _getAllIdsSelect($limit = null, $offset = null)
    {
        $idsSelect = parent::_getAllIdsSelect($limit, $offset);
        $idsSelect->resetJoinLeft();
        return $idsSelect;
    }

}

Note : please remove the generated folder and cleaned cache after replacing a code

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.