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

How to get the thread_id from the model

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 20, 2013.

  1. AndyB

    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?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  3. AndyB

    AndyB Well-Known Member

    Thank you, Chris. Your information is extremely helpful.

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

    Thread.php
    PHP:
    setcookie("threadId"$threadIdtime()+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
     
  4. AndyB

    AndyB Well-Known Member

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

    PHP:
    @$cookieThreadId $_COOKIE["threadId"];
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  6. AndyB

    AndyB Well-Known Member

    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
     
  7. Nobita.Kun

    Nobita.Kun Well-Known Member

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

    Chris D XenForo Developer Staff Member

    How are you setting $threadId?
     
  9. AndyB

    AndyB Well-Known Member

    Thank you, Nobita, that is very helpful.
     
  10. AndyB

    AndyB Well-Known Member

    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.
     
  11. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  12. AndyB

    AndyB Well-Known Member

    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.
     
  13. Chris D

    Chris D XenForo Developer Staff Member

    I already gave my suggestion. And cookies were nowhere near what I was thinking ;)
     
    Daniel Hood likes this.
  14. AndyB

    AndyB Well-Known Member

    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.
     
  15. AndyB

    AndyB Well-Known Member

    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.
     

Share This Page