• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Using the finder with whereOr and where

AndyB

Well-known member
#1
I'm trying to create a finder that selects certain threads. One of the criteria is to only select nodeIds if viewable, then further eliminate selections using the other ->where statements.

My question is the ->whereOr appears to override the ->where conditions below it. I suspect I need another AND somewhere but I'm not able to determine the correct syntax.

PHP:
$nodeRepo = $this->repository('XF:Node');
$nodes = $nodeRepo->getNodeList();
$nodeIds = $nodes->keys();

$conditions[] = ['node_id', $nodeIds];

$finder = \XF::finder('XF:Thread');
$results1 = $finder
    ->where('title', 'LIKE', $searchWord1, '%?%')
    ->where('title', 'LIKE', $searchWord2, '%?%')
    ->where('title', 'LIKE', $searchWord3, '%?%')
    ->where('discussion_state', '=', 'visible')
    ->where('discussion_state', '<>', 'redirect')
    ->where('post_date', '>=', $gte)
    ->where('thread_id', '<>', $currentThreadId)
    ->whereOr($conditions)
    ->order('post_date', 'DESC')
    ->limit($maximumResults1)->fetch()->toArray();
Thank you for your help.
 
Last edited:

AndyB

Well-known member
#2
The working query which I'm trying to replace is this:

PHP:
$results1 = $db->fetchAll("
SELECT thread_id
FROM xf_thread
WHERE title LIKE ?
AND title LIKE ?
AND title LIKE ?
AND discussion_state = 'visible'
AND discussion_type <> 'redirect'
AND post_date >= ?
AND thread_id <> ?
AND (node_id = 1 OR node_id = 2)
ORDER BY post_date DESC
LIMIT ?
", array("%$searchWord1%", "%$searchWord2%", "%$searchWord3%", $gte, $currentThreadId, $maximumResults1));

Please note the following AND is an example only, normally there's a variable which contains all the viewable nodes.

AND (node_id = 1 OR node_id = 2)
 
Last edited:

Snog

Well-known member
#3
We're getting back to people telling you to use a better IDE, or you don't understand what the IDE is telling you...
whereor.png

Try this...
Code:
$conditions = [['node_id', 1],['node_id', 2]];
If that works, it should be self explanatory what you did wrong or need to do (possibly in a different way).
 
Last edited:

AndyB

Well-known member
#4
Thank you for taking the time to look at this, Snog.

The original code I posted in post #1 works just fine, I was just tripped up on the $conditions variable being used later on.