AnsweredAssumed Answered

SugarQuery where or issue

Question asked by sidhu sidhu on Oct 11, 2017
Latest reply on Oct 12, 2017 by Alan Beam

Hi All,

 

              I am creating a duplicate script and retrieving the duplicates using the following query inside the findDuplicates function.

 

    public function findDuplicates()
    {
        if (empty($this->field) && empty($this->field1) && empty($this->field2)){
            return null;
        }

        $Query = new SugarQuery();
      
        $Query->from($this->bean);
       
        if(!empty($this->field))
        $Query->orWhere()->equals($this->field,$this->bean->{$this->field});
       
        if(!empty($this->field1))
        $Query->orWhere()->equals($this->field1,$this->bean->{$this->field1});
       
        if(!empty($this->field2))
        $Query->orWhere()->equals($this->field2,$this->bean->{$this->field2});
       
        $Query->limit(10);
        //Filter out the same Bean during Edits
        if (!empty($this->bean->id)) {
            $Query->where()->notEquals('id',$this->bean->id);
        }
        $sql= $Query->compileSql();
        _ppl("the query is ".$sql);
      
        $results = $Query->execute();
      
        return array(
            'records' => $results
        );
    }

 

 

Where as in the logs i am getting the where condition like below

 

WHERE accounts.deleted = 0 AND accounts.id != 'be415536-ae73-11b7-a64d-06ca2d54a2a4'accounts.name = 'jagga12' OR accounts.phone_office = '984803291' OR accounts.billing_address_street = 'and' LIMIT 0,10: MySQL error 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 'accounts.name = 'jagga12' OR accounts.phone_office = '984803291' OR accounts.bi' at line 1

 

I want to generate the mysql query like the below

WHERE accounts.deleted = 0 AND accounts.id != 'be415536-ae73-11b7-a64d-06ca2d54a2a4' AND accounts.name = 'jagga12' OR accounts.phone_office = '984803291' OR accounts.billing_address_street = 'and' 

 

Any suggestions please.

 


Regards

Sidhu André Lopes Francesca Shiekh

Outcomes