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

Better way to pull db table info for thread postbit.

Discussion in 'XenForo Development Discussions' started by mjda, Mar 3, 2013.

  1. mjda

    mjda Active Member

    Currently I'm trying to show some additional information about a user, which is stored in a custom db table, in the thread user postbit. I'm running an additional query for each "message_content" hook, but I'm wondering how it would be possible to just join my own data into the $hookParams['message'] array when it's set.
     
  2. digitalpoint

    digitalpoint Well-Known Member

    You want to extend the XenForo_Model_Thread class and specifically the prepareThreadFetchOptions() method... add your extra fields you want to SELECT as well as JOINs there.
     
    mjda likes this.
  3. mjda

    mjda Active Member

    Found it, thanks Shawn.
     
  4. mjda

    mjda Active Member

    Ok, so I've spent quite a bit of time (6+ hours) researching and reading about how to accomplish this and still have been unable to figure it out. I've tried several different approaches and still just am not able to get the data from 'mytable_user' to join with the user table. If someone could point me in the right direction, that would be great.

    Below is what I currently have in 2 files.

    I have a code event listener in 'load_class_model', which calles this method:

    PHP:
        public static function extendThreadModel($class, array &$extend)
        {
            if (
    $class == 'XenForo_Model_Thread')
            {
                
    $extend[] = 'myAddon_Model_Thread';
            }
        }
    Then, my myAddon_Model_Thread class looks like this:

    PHP:
    class myAddon_Model_Thread extends XFCP_myAddon_Model_Thread
    {
        public function 
    prepareThreadFetchOptions(array $fetchOptions)
        {
            
    $selectFields .= ', mytable_user.*';
            
    $joinTables .= 'LEFT JOIN mytable_user ON (user.user_id = mytable_user.user_id)';
            return array(
    'selectFields' => $selectFields'joinTables' => $joinTables);
        }
    }
     
  5. digitalpoint

    digitalpoint Well-Known Member

    Well one thing I notice is your prepareThreadFetchOptions() isn't extending the existing method, rather it's overwriting it... you should do something like this in the beginning of it:

    PHP:
    $response parent::prepareThreadFetchOptions($fetchOptions);
    Then append your stuff to the $response array.
     
    Lucas and mjda like this.
  6. mjda

    mjda Active Member

    Doh! Thanks again for the help, Shawn. I'll give that a go then and see what I can come up with.
     
  7. mjda

    mjda Active Member

    Finally got this thing working. Thanks again, Shawn.
     
  8. mjda

    mjda Active Member

    New problem. I actually did end up getting what I needed to work with the XenForo_Model_Post class, and it seems to be working but with 1 problem (that I've been able to find). When I hover over 'Alerts' I get an error box that says "Undefined index: node_id". I'm not even sure where this is coming from to be honest, but it has to be something I'm doing because if I disable the addon the error goes away. Here is a look inside my extended XenForo_Model_Post class.

    PHP:
    class myAddon_Model_Post extends XFCP_myAddon_Model_Post
    {
        public function 
    preparePostJoinOptions(array $fetchOptions)
        {
            if (!empty(
    $fetchOptions['join']))
            {
                if (
    $fetchOptions['join'] & self::FETCH_USER)
                {
                    
    $response parent::preparePostJoinOptions($fetchOptions);
                    
    $response['selectFields'] .= ', mytable_user.*';
                    
    $response['joinTables'] .= 'LEFT JOIN mytable_user ON (mytable_user.user_id = post.user_id)';
                    return 
    $response;
                }
            }
        }
    }
     
  9. digitalpoint

    digitalpoint Well-Known Member

    The parent line and return line should be outside of your if statements... They should happen 100% of the time.
     
    mjda likes this.
  10. mjda

    mjda Active Member

    Right again. Thanks, yet again.
     

Share This Page