Better way to pull db table info for thread postbit.

mjda

Well-known 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.
 
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.
 
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);
    }
}
 
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.
 
Doh! Thanks again for the help, Shawn. I'll give that a go then and see what I can come up with.
 
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;
            }
        }
    }
}
 
Back
Top Bottom