XF 2.1 Filter by 2 different fields

Cupara

Well-known member
On an admin page, I have 2 fields that I need the filter to search through and return any results.

I have this:
PHP:
$finder->where('home_team', 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%'));

It works perfect but if I do the following:
PHP:
$finder->where(['home_team', 'away_team'], 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%'));
The above returns an error of using an array instead of string.

Or:
PHP:
$finder->where('home_team', 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%'))->where('away_team', 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%'));
The above returns 0 matches which I know there are matches.
 
Then you'd need to use whereOr() as @Bespoke suggested, otherwise it will only return results where both have matches. Something like:

PHP:
$finder->whereOr([
    ['home_team', 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%')],
    ['away_team', 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%')]
]);
 
Then you'd need to use whereOr() as @Bespoke suggested, otherwise it will only return results where both have matches. Something like:

PHP:
$finder->whereOr([
    ['home_team', 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%')],
    ['away_team', 'LIKE', $finder->escapeLike($filter['text'], $filter['prefix'] ? '?%' : '%?%')]
]);
I've tried that but got an error. I'll try it again, maybe I did something wrong the first time. I'll post back results.
 
Back
Top Bottom