XF 2.2 adding FirstPost to API results

bob2bob

Member
We're extending the API so that we can grab watched threads and we'd like to also include the first_post from each thread.

You can see what I'm doing here:
PHP:
class DNWatchedThreads extends AbstractController
{
    public function actionGet()
    {
        $page = $this->filterPage();
        $perPage = 10;
 
        $visitor = \XF::visitor();
 
        /** @var \XF\Repository\Thread $threadRepo */
        $threadRepo = $this->repository('XF:Thread');
        $threadFinder = $threadRepo->findThreadsForApi();
 
        $threadFinder->with('Watch|' . $visitor->user_id, true);
        $threadFinder->with('fullForum');
        $threadFinder->with('FirstPost'); // TODO: get this to include the first post of the thread
        $threadFinder->where('discussion_state', 'visible');
        $threadFinder->setDefaultOrder('last_post_date', 'DESC');
 
        $total = $threadFinder->total();
 
        $this->assertValidApiPage($page, $perPage, $total);
 
        $threads = $total > 0
            ? $threadFinder->limitByPage($page, $perPage)->fetch()
            : $this->em()->getEmptyCollection();
        if (\XF::isApiCheckingPermissions()) {
            $threads = $threads->filterViewable();
        }
 
        $data = [
            'threads' => $threads->toApiResults(),
            'pagination' => $this->getPaginationData($threads, $page, $perPage, $total)
        ];
 
        return $this->apiResult($data);
    }
}


$threadFinder->with('FirstPost'); this line does not seem to be working though it appears to be the right way of doing it based on the documentation here: https://xenforo.com/docs/dev/entities-finders-repositories/

anybody have any ideas where we're going wrong?
 

Similar threads

Replies
1
Views
2K
Top Bottom