How to get the thread_id from the model

AndyB

Well-known member
In my model I'm able to get the visitor userId using the following code:

PHP:
$visitor = XenForo_Visitor::getInstance();  
$userId = $visitor['user_id'];

What I would like to be able to do is get the thread_id, but nothing I try works. Is it possible to get the thread_id of the last visited thread?
 
That information isn't really stored permanently. Certainly not in the xf_user_* tables. The closest is the xf_session_activity table but that information is not permanent and is routinely pruned. It also includes only the very last activity (could be a specific thread, forum, or other page).

The only way to do this, then, is to extend XenForo to specifically store that information. To save additional queries, I'd personally recommend storing it in a new column in the xf_user table which is then automatically available in the $visitor object.

As a point in the right direction, you could think about extending the Thread controller, or, what might be more suitable is to think about extending the Session model.
 
Thank you, Chris. Your information is extremely helpful.

What I'm trying now is to use a cookie.

Thread.php
PHP:
setcookie("threadId", $threadId, time()+3600*24*365*1, '/');

Model.php
PHP:
$cookieThreadId = $_COOKIE["threadId"];

The code works fine except in my server error logs I get the following error message:

Undefined index: threadId
 
Looks like if I put a @ in front of the code it suppresses the error message.

PHP:
@$cookieThreadId = $_COOKIE["threadId"];
 
That's really bad practice. It's not a solution. It's like knowing there's a hole in the wall and putting a picture up over it to cover it.

PHP:
if ($threadId)
{
    // do something
}

Is a much better idea.
 
Hi Chris,

I wish that code would work. Unfortunately when I try to reference $threadId I get the following error when I try to view a thread:

Undefined variable: threadId
 
I think error that line:
Code:
@$cookieThreadId = $_COOKIE["threadId"];
So should be
Code:
if(isset($_COOKIE["threadId"]))
{
$threadId= $_COOKIE["threadId"];
}
else
{
$threadId = false;
}
 
Hi Chris,

I wish that code would work. Unfortunately when I try to reference $threadId I get the following error when I try to view a thread:

Undefined variable: threadId
How are you setting $threadId?
 
How are you setting $threadId?

Hi Chris,

I was setting the threadId in the function like this:

PHP:
public function getThreads($safeSearchWord1,$safeSearchWord2,$safeSearchWord3,$threadId)

However I was experiencing on occasion where $threadId would get a new value and new posts were being added to the incorrect thread. So I wanted to do away with using the $threadId being passed into the function.
 
The undefined variable error suggests that $threadId isn't being set at all so you've got some problems further up your code if that's the case. Whatever function you're using to set $threadId in the first place isn't actually setting the $threadId.

It's very hard to continue to help you without seeing more of your code.
 
Turns out that using cookies is not the solution. Because the way threads are displayed, I'm only setting the cookie when the thread is displayed, so only if I reload the page does the cookie take effect.
 
I'm going to put the thread_id back into the function, but this time I'll use a variable $currentThreadId so there's no chance of the $threadId from getting a new value and having a new post go to the wrong thread.

$currentThreadId = $threadId;

Hopefully that will solve the problem I was having.

Thank you, Chris.
 
Turns out the problem I was having was using the variable $threads. This is not a good name to use as a variable because XenForo is already using it. I changed the variable to a unique name ($similarThreads) and the issue I was having went away. Lesson learned, do not use the variable name $threads or $thread in your add-ons.
 
Back
Top Bottom