1

I'm trying to get the value based on the field input like email, contactno on the input field in frontend, my getagentdata.php 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->addFieldToSelect("email")
                    ->addFieldToFilter(
                        [
                            ["field"=>"email", "like"=>"%".$searchQuery."%"],
                            ["field"=>"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);
    }
}
}

But magento2 returns this error.

{"success":1,"message":"Warning: Illegal offset type in isset or empty in /var/www/sqdelivery/vendor/magento/framework/Data/Collection/AbstractDb.php on line 423","agent":[]}

if i comment the below else part, Error is not coming

else {
            $agentCollection->addFieldToSelect("email")
                ->addFieldToFilter(
                    [
                        ["field"=>"email", "like"=>"%".$searchQuery."%"],
                        ["field"=>"contactno", "like"=>"%".$searchQuery."%"]
                    ]
                );
        }

I think problem is on the addFieldToFilter & addFieldToSelect.

Please help me to solve this error. Thanks in advance.

2
  • try after disable this setOrder function.
    – kunj
    Sep 29, 2018 at 14:22
  • @kunj see the updated question bro.. problem is in addFieldToFilter or addFieldtoSelect.
    – Ask Xah
    Sep 29, 2018 at 14:28

2 Answers 2

0

Try this:

    $agentCollection->addFieldToSelect("email") ->addFieldToFilter( "email",["like"=>"%".$searchQuery."%"])
->addFieldToFilter("contactno",["like"=>"%".$searchQuery."%"]);
7
  • this also returns the same error bro..
    – Ask Xah
    Sep 30, 2018 at 11:01
  • Ok, try this echo $agentCollection->getSelect()->__toString(); and run that query in mysql. check that query working or not
    – kunj
    Oct 1, 2018 at 5:45
  • i had get this on echo, {"success":1,"message":"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{firstname}})), ' ', LTRIM(RTRIM({{lastname}}))) AS id FROM pos_agent AS ma' at line 1, query was: SELECT main_table.*, CONCAT(LTRIM(RTRIM({{firstname}})), ' ', LTRIM(RTRIM({{lastname}}))) AS id` FROM pos_agent AS main_table WHERE ((email = '') OR (contactno = '')) ORDER BY main_table.id DESC\n","agent":[]}
    – Ask Xah
    Oct 1, 2018 at 8:33
  • I think you need to correct this code $agentId = $wholeData["agentId"] ?? 0; $searchQuery = $wholeData["searchQuery"] ?? "";
    – kunj
    Oct 1, 2018 at 10:40
  • i had cleared the error. problem in the collection.solved. but now if i echo the agentcollection it return the echo like this " SELECT main_table.email FROM pos_agent AS main_table WHERE ((email = '') OR (contactno = '') OR (firstname = '') OR (lastname = ''))"
    – Ask Xah
    Oct 1, 2018 at 11:01
0
$agentCollection-> addFieldToFilter(
    [
     ["email", "contactno"],
     ["like" => "%{$searchQuery}%"], ["like" => "%{$searchQuery}%"]
    ]
);
1
  • it works bro. but returns the "SQLSTATE[42000]: Syntax error or access violation: 1064"
    – Ask Xah
    Oct 1, 2018 at 10:09

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.