Skip to content
Advertisement

FluentPDO cant seem to find a way of doing OR clause

So I have been using FluentPDO quite a bit, BUT I can’t seem to find a way of doing an OR clause, the where auto adds the AND and I don’t see a way around it.

That is the type of SQL statement I need to create.

SELECT *
FROM `items` 
WHERE `item_var` LIKE '%test%' 
OR `item_text` LIKE '%test%' 
OR `item_int` LIKE '%test%' 
OR `item_select` LIKE '%test%' 
ORDER BY row_id

Advertisement

Answer

I’m not familiar with the library, but after looking into the source, it doesn’t seem like this is easily possible without placing your OR code into the where() call manually.

According to the where method’s docblock, it will prepend each successive clause with an AND.

It’s not the most expressive code in the world, but it appears you would have to put your ORs in manually:

->where('(item_text LIKE "%test%" OR item_int LIKE "%test%" OR item_select LIKE "%test%")')

As long as you are using only OR, you might be able to clean it up a bit by putting them into an array and implode()ing them. Perhaps something like this would work:

$conditions = [
    'item_text LIKE "%test%"',
    'item_int LIKE "%test%"',
    'item_select LIKE "%test%"',
];

$orWhere = implode(" OR ", $conditions);
$query = $fpdo->from('items')->where("($orWhere)");

(Edit: grammar)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement