Idhae
Active member
Hello,
im trying to do something like the vBulletin Stock feature. i put an attachment icon to these threads who include an attachment and count them.
vBulletin:
Xenforo2:
So far so good, but i did this so ->
i extend "XF\Entity\Thread" with a new "getter attach_count" and a "repository" to get the count for each thread
as next i have done a TMS on "thread_list_macros" and call to the getter $thread.attach_count and it works fine but i have logically for each thread one query...thats really bad. On a "forum_view" with 20 threads per page it calls 20querys only for the "count".
I now try to figure out how i can do this in "XF\Pub\Controller\Forum actionForum" with one query and save it in $threads so i could use it then in the template.
repository:
I hope someone help or give an idea how i could do this.
EDIT: now it works
im trying to do something like the vBulletin Stock feature. i put an attachment icon to these threads who include an attachment and count them.
vBulletin:
Xenforo2:
So far so good, but i did this so ->
i extend "XF\Entity\Thread" with a new "getter attach_count" and a "repository" to get the count for each thread
as next i have done a TMS on "thread_list_macros" and call to the getter $thread.attach_count and it works fine but i have logically for each thread one query...thats really bad. On a "forum_view" with 20 threads per page it calls 20querys only for the "count".
I now try to figure out how i can do this in "XF\Pub\Controller\Forum actionForum" with one query and save it in $threads so i could use it then in the template.
PHP:
<?php
namespace MY\ADDON\XF\Pub\Controller;
use XF\Mvc\ParameterBag;
class Forum extends XFCP_Forum
{
public function actionForum(ParameterBag $params)
{
$parent = parent::actionForum($params);
$threads = $parent->getParam('threads');
$threadsIds = $threads->pluckNamed('thread_id');
if($threadsIds){
$threadsRepo = $this->repository('MY\ADDON:Thread');
$attachCounts = $threadsRepo->findAttachmentCountsInThreads($threadsIds);
foreach ($threads as $key => $thread) {
$thread->attachCount = $attachCounts[$key]['attach_count'];
}
$parent->setParam('threads', $threads);
}
return $parent;
}
}
repository:
PHP:
<?php
namespace MY\ADDON\Repository;
use XF\Mvc\Entity\Repository;
class Thread extends Repository
{
public function findAttachmentCountsInThreads($threadsIds)
{
$counts = $this->db()->fetchAllKeyed('
SELECT thread_id, SUM(attach_count) AS attach_count
FROM xf_post
WHERE thread_id IN (' . implode(',', $threadsIds) . ')
GROUP BY thread_id
', 'thread_id');
return $counts;
}
}
I hope someone help or give an idea how i could do this.
EDIT: now it works
Last edited: