MeDoo - Conditional LIKE in the WHERE part

342 Views Asked by At

How can I write the follow sql query in MeDoo???

SELECT * FROM lockers WHERE Active = 1 AND GymID = " . $gimid . " AND ForMale = " . $male . 
                    ($filter ? " AND Locker LIKE '%" . $filter . "%'" : "")

The problem for me is the conditional LIKE.

$total = $this->db->count('lockers', 
     ['AND' => [
          'Active' => 1, 
          'GymID' => $gimid, 
          'ForMale' => $male
     ]]
);

Some suggestion???? Thanks a lot!!!

1

There are 1 best solutions below

0
Silver Lora On

Finally, I solved it in the follow way:

// getting locker's total
$total = $this->db->debug()->count('lockers', [
    'AND' => [
        'Active' => 1, 
        'GymID' => $gimid, 
        'ForMale' => $male,
        'Locker[~]' => $filter ? $filter : '' //<- LIKE with empty string shows all
    ]
]);