XF 2.2 Finder total() returns 0

Finexes

Well-known member
Hey guys,

I'm trying to get the number of users with XenForo's finder, but it always returns 0:
PHP:
$finder = \XF::finder('XF:User');
$countUsers = $finder->where('custom_title', 'LIKE', 'String%')->total();
// returns 0

Although the same query returns the correct amount in phpMyAdmin:
SQL:
SELECT * FROM `xf_user` WHERE `custom_title` LIKE 'String%';

// returns 3 results

What am I doing wrong?
 
Think it should be

$finder->where('custom_title', 'LIKE', 'String%')->fetch()->total();

The key difference being the added ->fetch()
 
Thanks, but the issue was somewhere else. I had to change the code to:

PHP:
$countUsers = \XF::finder('XF:User')->where('custom_title', 'LIKE', 'String%')->total();

I am using the finder multiple times and somehow it only returns a result for the first query, when defining $finder = \XF::finder('XF:User'); and reusing the $finder variable.
 
PHP:
$text = 'String';

$finder = \XF::finder('XF:User')
    ->where('user_group_id', 2)
;
$finder->where('custom_title', 'LIKE', $finder->escapeLike($text, '?%'));

$countUsers = $finder->total();
 
Back
Top Bottom