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

XF 2.0 How do I create a new entity?

AndyB

Well-known member
#1
I'm updating my History add-on from XF1. What I would like to do is be able to click a link called History and rather then showing "Your threads" as seen here:

1507731979259.png

I would like to show threads most recently viewed by the member.

I have successfully extended the FindThreads class and I can view the same page using my function.

I have created a table called xf_history, these are the fields:

1507732146473.png

The code I think I will need to use is this:

PHP:
$finder = \XF::finder('XF:History')
    ->where('user_id', '=', $userId);
       
$threadFinder = $finder->fetch();
I assume this will work and all the finder needs is the thread_id's. So if this is all correct, my assumption is I need to create an entity for History. How do I create a History entity? I know how to extend an entity for example when I add a column to an existing table, is it basically the same procedure?
 

Chris D

XenForo developer
Staff member
#2
I'm updating my History add-on from XF1. What I would like to do is be able to click a link called History and rather then showing "Your threads" as seen here:

View attachment 159578

I would like to show threads most recently viewed by the member.

I have successfully extended the FindThreads class and I can view the same page using my function.

I have created a table called xf_history, these are the fields:

View attachment 159579

The code I think I will need to use is this:

PHP:
$finder = \XF::finder('XF:History')
    ->where('user_id', '=', $userId);
      
$threadFinder = $finder->fetch();
I assume this will work and all the finder needs is the thread_id's. So if this is all correct, my assumption is I need to create an entity for History. How do I create a History entity? I know how to extend an entity for example when I add a column to an existing table, is it basically the same procedure?
You know XF already stores that data, right? Not sure why it would need a separate table.

You could just find threads which has read marking data for the current visitor and use that.
 

AndyB

Well-known member
#3
You know XF already stores that data, right?
Great idea! :)

Yes I'll just additionally use the xf_thread_read table data to grab the thread IDs I would like to show.

Question, how can I modify this code to include the xf_thread_read condition?

PHP:
$this->finder('XF:Thread')
            ->forFullView(true)
            ->with(['Forum', 'User'])
            ->exists('UserPosts|' . $userId)
            ->where('discussion_type', '<>', 'redirect')
            ->setDefaultOrder('last_post_date', 'DESC')
            ->indexHint('FORCE', 'last_post_date');
Thank you.
 

AndyB

Well-known member
#4
Looking at the XenForo code I'm trying to figure out the above post question. I ran across this code and I'm not understanding where "expression" is defined:

1507738836004.png

What the purpose of "expression" and where is it defined?

Thank you.
 

AndyB

Well-known member
#5
when I include the code in the previous post, I get the following error:

Code:
An exception occurred: [Error] Call to undefined method XF\Pub\App::expression() in src/addons/Andy/History/XF/Pub/Controller/FindThreads.php on line 24
 

AndyB

Well-known member
#6
This code works, very well.

PHP:
<?php

namespace Andy\History\XF\Pub\Controller;

class FindThreads extends XFCP_FindThreads
{
    public function actionHistory()
    {    
        // get userId
        $userId = $this->getUserId();
        
        // get db
        $db = \XF::db();

        // run query
        $results = $db->fetchAll("
        SELECT xf_thread.thread_id
        FROM xf_thread_read
        INNER JOIN xf_thread ON xf_thread.thread_id = xf_thread_read.thread_id
        WHERE xf_thread_read.user_id = ?
        AND xf_thread.discussion_state = ?
        ORDER BY xf_thread_read.thread_read_date DESC
        LIMIT ?
        ", array($userId,'visible',50));    
        
        // get conditions
        foreach ($results AS $k => $v)
        {
            $threadId = $v['thread_id'];
            $conditions[] = ['thread_id', '=', $threadId];
        }

        // get threads
        $finder = \XF::finder('XF:Thread');
        $threads = $finder->whereOr($conditions)->fetch();        

        // get pageSelected
        $pageSelected = 'history';
        
        // get viewParams
        $viewParams = [
            'threads' => $threads,
            'user' => $this->user,
            'pageSelected' => $pageSelected,
        ];
        
        // send to template
        return $this->view('XF:FindThreads\List', 'find_threads_list', $viewParams);
    }
}
 
Last edited: