XF 2.0 Finder ->with command

AndyB

Well-known member
I'm trying to create a finder which searches the xf_post table.

The following $conditions variable:

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

foreach ($nodeIds AS $k => $v)
{
    $conditions[] = ['xf_thread.node_id', '=', $v];
}


The finder:

PHP:
$finder = \XF::finder('XF:Post');
$posts = $finder
    ->with('Thread')
    ->where('message', 'LIKE', $finder->escapeLike($keywords, '%?%'))
    ->where('message_state', '=', 'visible')
    ->whereOr($conditions)
    ->order('post_id', 'DESC')
    ->limit($limit)
    ->fetch();


Why am I getting the following error message?

HTML:
LogicException: Unknown relation XF:Thread accessed on xf_post in src/XF/Mvc/Entity/Finder.php at line 630


Thank you.
 
Now I would like to also add Node as a relation. The reason is in my template I would like to show the forum title.

I added ->with('Node') like this:

PHP:
$finder = \XF::finder('XF:Post');
$posts = $finder
    ->with('Thread')
    ->with('Node')
    ->where('message', 'LIKE', $finder->escapeLike($keywords, '%?%'))
    ->where('message_state', '=', 'visible')
    ->whereOr($conditions)
    ->order('post_id', 'DESC')
    ->limit($limit)
    ->fetch();


but I get the following error:

HTML:
LogicException: Unknown relation Node accessed on xf_post in src/XF/Mvc/Entity/Finder.php at line 630


Thank you.
 
Try

PHP:
$finder = \XF::finder('XF:Post');
$posts = $finder
    ->with('Thread')
    ->with('Thread.Node')
    ->where('message', 'LIKE', $finder->escapeLike($keywords, '%?%'))
    ->where('message_state', '=', 'visible')
    ->whereOr($conditions)
    ->order('post_id', 'DESC')
    ->limit($limit)
    ->fetch();
 
Thank you, XenConcept. However that doesn't work, I get the following error:

HTML:
LogicException: Unknown relation Node accessed on xf_thread in src/XF/Mvc/Entity/Finder.php at line 630
 
Excuse me I was wrong.

PHP:
$finder = \XF::finder('XF:Post');
$posts = $finder
    ->with('Thread')
    ->with('Thread.Forum')
   ->with('Thread.Forum.Node')
    ->where('message', 'LIKE', $finder->escapeLike($keywords, '%?%'))
    ->where('message_state', '=', 'visible')
    ->whereOr($conditions)
    ->order('post_id', 'DESC')
    ->limit($limit)
    ->fetch();
 
Top Bottom