Preparing a statement, etc.

Valhalla

Well-known member
Hello,

I'm extending the prepareForumJoinOptions function (XenForo_Model_Forum) to include my own entries to supplement a query.

I have two questions:

I have my additions to the prepareForumJoinOptions function set up. I only want my specific bits (selectFields / joinTables) to be in use when viewing the forum list, this means not the forum view. I had though about using an event hint to limit the listener to only one controller, but I'm not sure that would work because "XenForo_ControllerPublic_Forum" applies to both forum list and forum view. I would only require forum list (index). How would I specify this? Could I specify which controller action it should apply for (Index)?

The second question: the query I am creating requires "GROUP BY forum.node_id" to be appended to the end, but I cannot use either "selectFields" or "joinTables" for this purpose. You can see in the code below, I have added this section in the place it would go. I was thinking of extending this by passing the $joinOptions through the parent, to allow for other add-ons which may require it. Then, return the final statement (as below) with my additions. Does anyone have any ideas?

This is the function from Model_Forum which has my own GROUP BY part on the end:

PHP:
    public function getExtraForumDataForNodes(array $nodeIds, array $fetchOptions = array())
    {
        if (!$nodeIds)
        {
            return array();
        }

        $joinOptions = $this->prepareForumJoinOptions($fetchOptions);

        return $this->fetchAllKeyed('
            SELECT forum.*
                ' . $joinOptions['selectFields'] . '
            FROM xf_forum AS forum
            INNER JOIN xf_node AS node ON (node.node_id = forum.node_id)
            ' . $joinOptions['joinTables'] . '
            WHERE forum.node_id IN (' . $this->_getDb()->quote($nodeIds) . ')
        GROUP BY forum.node_id', 'node_id');
    }

My extended function would be similar to:

PHP:
    public function getExtraForumDataForNodes(array $nodeIds, array $fetchOptions = array())
    {
        $parent = parent::getExtraForumDataForNodes($nodeIds, $fetchOptions);

        return $this->fetchAllKeyed('
            SELECT forum.*
                ' . $parent['selectFields'] . '
            FROM xf_forum AS forum
            INNER JOIN xf_node AS node ON (node.node_id = forum.node_id)
            ' . $parent['joinTables'] . '
            WHERE forum.node_id IN (' . $this->_getDb()->quote($nodeIds) . ')
        GROUP BY forum.node_id', 'node_id');
    }



Thank you!
 
Last edited:
For my second problem, this works fine without any problems that I know of.

PHP:
public function getExtraForumDataForNodes(array $nodeIds, array $fetchOptions = array())
{
    $joinOptions = $this->prepareForumJoinOptions($fetchOptions);

    return $this->fetchAllKeyed('
        SELECT forum.*
            ' . $joinOptions['selectFields'] . '
        FROM xf_forum AS forum
        INNER JOIN xf_node AS node ON (node.node_id = forum.node_id)
        ' . $joinOptions['joinTables'] . '
        WHERE forum.node_id IN (' . $this->_getDb()->quote($nodeIds) . ')
    GROUP BY forum.node_id', 'node_id');

    return parent::getExtraForumDataForNodes($nodeIds, $fetchOptions);

}
 
For my second problem, this works fine without any problems that I know of.

PHP:
public function getExtraForumDataForNodes(array $nodeIds, array $fetchOptions = array())
{
    $joinOptions = $this->prepareForumJoinOptions($fetchOptions);

    return $this->fetchAllKeyed('
        SELECT forum.*
            ' . $joinOptions['selectFields'] . '
        FROM xf_forum AS forum
        INNER JOIN xf_node AS node ON (node.node_id = forum.node_id)
        ' . $joinOptions['joinTables'] . '
        WHERE forum.node_id IN (' . $this->_getDb()->quote($nodeIds) . ')
    GROUP BY forum.node_id', 'node_id');

    return parent::getExtraForumDataForNodes($nodeIds, $fetchOptions);

}

This could break any addons that extends to Model_Forum (depend on Callback Execution Order and Event Hint) and use that function because it always return your query
 
This could break any addons that extends to Model_Forum (depend on Callback Execution Order and Event Hint) and use that function because it always return your query

You're right - plus I've just realised I'm trying to return both the query and the parent.

Will have to think again, thanks. :-)
 
Back
Top Bottom