XF 2.2 count attachment

MesterPerfect

Active member
I can fetch number of resources and number downloads using this code
PHP:
        $db = \XF::db();
    
        // run query
        $totalResources = $db->fetchOne("
        SELECT COUNT(resource_id)
        FROM xf_rm_resource
        ");
    
        // run query
        $totalDownloads = $db->fetchOne("
        SELECT SUM(download_count)
        FROM xf_rm_resource
        ");

But I cannot modify it with attachments
 
Attachments are recorded in a separate table.
See
xf_attachment
xf_attachment_data
xf_attachment_view

Edit:
Looks like the resource_update table keeps track of attachment count
 
Last edited:
Attachments are recorded in a separate table.
See
xf_attachment
xf_attachment_data
xf_attachment_view

Edit:
Looks like the resource_update table keeps track of attachment count
I modified ForumStatistics.php file
PHP:
<?php

namespace XF\Widget;

class ForumStatistics extends AbstractWidget
{
    public function render()
    {


    // get db
        $db = \XF::db();
    
        // run query
        $totalResources = $db->fetchOne("
        SELECT COUNT(resource_id)
        FROM xf_rm_resource
        ");
    
        // run query
        $totalDownloads = $db->fetchOne("
        SELECT SUM(download_count)
        FROM xf_rm_resource
        ");

        $totalAttachmensah = $db->fetchOne("
        SELECT COUNT(attachment_id)
        FROM xf_attachment
        ");



        $viewParams = [


'forumStatistics' => $this->app->forumStatistics,
      'totalResources' => $totalResources,
            'totalDownloads' => $totalDownloads


        ];
        return $this->renderer('widget_forum_statistics', $viewParams);
    }

    public function getOptionsTemplate()
    {
        return null;
    }
}

When I add the variable like this it gives me an error in community

PHP:
'forumStatistics' => $this->app->forumStatistics,
      'totalResources' => $totalResources,
            'totalDownloads' => $totalDownloads
      'totalAttachmensah' => $totalAttachmensah,
 
SELECT SUM(attach_count)
FROM xf_rm_resource_update

Or count attachments directly from the attachments table by type.

But you'll want to cache the results - so you are better off modifying the getForumStatisticsCacheData function in class Repository\Counters as that saves the values in the Registry
 
Top Bottom