0

I'm trying to get a Array Response from collection with getfieldset like below, still got this error and unable to resolve this.

Uncaught Error: Cannot instantiate interface Magento\Framework\DB\Adapter\AdapterInterface in /var/www/sqdelivery/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:50 Stack trace: #0 /var/www/sqdelivery/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\Framewo...') #1 /var/www/sqdelivery/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(144): Magento\Framework\ObjectManager\ObjectManager->get('Magento\Framewo...') #2 /var/www/sqdelivery/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(230): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\Framewo...', NULL, 'AdapterInterfac...', 'Webkul\Pos\Mode...') #3 /var/www/sqdelivery/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime('Webkul\Pos\Mode...', Array, Array) #4 /var/www/ in /var/www/sqdelivery/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php on line 50

My collection file is below,

<?php
namespace Webkul\Pos\Model\ResourceModel\Agents;
use 
Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
/**
 * Name of collection model
 */
const AGENT_MODEL_NAME = Webkul\Pos\Model\ResourceModel\Agents::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 $AdapterInterface,
    \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
    $modelName = self::AGENT_MODEL_NAME
) {
    $this->_fieldsetConfig = $fieldsetConfig;
    $this->_modelName = $modelName;
    parent::__construct(
        $entityFactory,
        $logger,
        $fetchStrategy,
        $eventManager,
        $$AdapterInterface,
        $resource,
        $universalFactory,
        $entitySnapshot,
        $connection
    );
}

/**
 * Resource initialization
 *
 * @return void
 */
protected function _construct()
{
    $this->_init($this->_modelName, Webkul\Pos\Model\ResourceModel\Agents::class);
}

/**
 * Group result by customer email
 *
 * @return $this
 */
public function groupByEmail()
{
    $this->getSelect()->from(
        ['email' => $this->getEntity()->getEntityTable()],
        ['email_count' => new \Zend_Db_Expr('COUNT(email.id)')]
    )->where(
        'email.id = e.id'
    )->group(
        'email.email'
    );

    return $this;
}

/**
 * Add Name to select
 *
 * @return $this
 */
public function addNameToSelect()
{
    $fields = [];
    $customerAccount = $this->_fieldsetConfig->getFieldset('agent_account');

   /* print_r($customerAccount);
    exit; */

    foreach ($customerAccount as $code => $field) {
        if (isset($field['name'])) {
            $fields[$code] = $code;

        }
    }

    $connection = $this->getConnection();
    $concatenate = [];

    $concatenate[] = 'LTRIM(RTRIM({{firstname}}))';

    $concatenate[] = 'LTRIM(RTRIM({{lastname}}))';


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

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

    return $this;
}

/**
 * Get SQL for get record count
 *
 * @return \Magento\Framework\DB\Select
 */
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;
}
}

And my Fieldset.xml is below

<?xml version="1.0"?>
  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
  <scope id="global">
     <fieldset id="agent_account">
        <field name="firstname">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>

        <field name="lastname">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>
        <field name="email">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>
        <field name="contactno">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>
        <field name="city">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>
        <field name="type">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>
        <field name="agent_info">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>
        <field name="id">
            <aspect name="create" />
            <aspect name="update" />
            <aspect name="name" />
        </field>

    </fieldset>
</scope>
</config>

And my getAgentdata.php file is below,

<?php
 namespace Webkul\Pos\Controller\Index;

 class GetAgentData extends AbstractPos
{

public function execute()
{
    $returnArray             = [];
    $returnArray["success"]  = 1;
    $returnArray["message"]  = "";
    $returnArray["agent"] = [];
    try  {
        if ($this->_coreSession->getIsLoggedin()) {
            $wholeData   = $this->getRequest()->getParams();
            $agentId  = $wholeData["agentId"]  ?? 0;
            $searchQuery = $wholeData["searchQuery"] ?? "";
            $searchQuery = trim($searchQuery);
            $agentCollection = $this->_agents->create()->addNameToSelect();

            if ($agentId > 0) {
                $agentCollection->addFieldToFilter("id", $agentId);
            } else {
                $agentCollection->addAttributeToSelect("email")
                    ->addAttributeToFilter(
                        [
                            ["attribute"=>"email", "like"=>"%".$searchQuery."%"],
                            ["attribute"=>"contactno", "like"=>"%".$searchQuery."%"]
                        ]
                    );
            }
            $agentCollection->setOrder("id", "DESC");
            $agentData = [];
            foreach ($agentCollection as $agent) {
                $eachAgentData = [];
                $eachAgentData = $agent->getData();
                $eachAgentData["id"] = (int)$eachAgentData["id"];
                $eachAgentData["firstname"] = $eachAgentData["firstname"];
                $eachAgentData["lastname"] = $eachAgentData["lastname"];
                $eachAgentData["contactno"] = $eachAgentData["contactno"];
                unset($eachAgentData["is_active"]);
                unset($eachAgentData["created_at"]);
                unset($eachAgentData["updated_at"]);
                unset($eachAgentData["password"]);


                $returnArray["agent"][] = $eachAgentData;
                //$eachAgentData->debug();
            }
            $returnArray["success"] = 1;
        } else {
            $returnArray["message"] = __("Session Expired.");
        }
        return $this->getJsonResponse($returnArray);
    } catch(\Exception $e) {
        $returnArray["message"] = $e->getMessage();
        return $this->getJsonResponse($returnArray);
    }
}
}

I nee this type of Output Response, enter image description here

Please help me to resolve this error. Thanks in Advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.