XF 2.1 Filter by 2 different fields

Cupara

Well-known member
Licensed customer
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.
 
The above returns 0 matches which I know there are matches.
Are you after results where both home_team and away_team match the search or are you after results where either one have a match?
I'm wondering if you are after a whereOr rathet than two where clauses?
 
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