Fixed addAttachmentsToContent expects array key to be always content id

Affected version
XF 2.0

TickTackk

Well-known member
In file xf2\src\XF\Repository\Attachment.php method addAttachmentsToContent() expects the the key to be always the content id. Think the fixed version could be this:
PHP:
    public function addAttachmentsToContent($content, $contentType, $countKey = 'attach_count', $relationKey = 'Attachments')
    {
        /** @var ArrayCollection $content */
        $ids = [];
        /** @var \XF\Mvc\Entity\Entity $item */
        foreach ($content AS $id => $item)
        {
            if ($item->{$countKey})
            {
                if ($item instanceof \XF\Mvc\Entity\Entity)
                {
                    $ids[] = $item->getEntityId();
                }
                else
                {
                    $ids[] = $id;
                }
            }
        }

        if ($ids)
        {
            $attachments = $this->finder('XF:Attachment')
                ->where([
                    'content_type' => $contentType,
                    'content_id' => $ids
                ])
                ->order('attach_date')
                ->fetch()
                ->groupBy('content_id');

            foreach ($content AS &$item)
            {
                $contentId = $item->getEntityId();
                $contentAttachments = isset($attachments[$contentId]) ? $this->em->getBasicCollection($attachments[$contentId]) : $this->em->getEmptyCollection();
                if ($item instanceof \XF\Mvc\Entity\Entity)
                {
                    $item->hydrateRelation($relationKey, $contentAttachments);
                }
                else
                {
                    $item[$relationKey] = $contentAttachments->toArray();
                }
            }
        }

        return $content;
    }
 
Last edited:

Chris D

XenForo developer
Staff member
I think the code you provided can be simplified in a few places.

Currently going with the following; could you check to ensure it still fits your use case?

Diff:
Index: src/XF/Repository/Attachment.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/XF/Repository/Attachment.php    (date 1535250218000)
+++ src/XF/Repository/Attachment.php    (date 1535251077000)
@@ -162,14 +162,22 @@
         $db->emptyTable('xf_attachment_view');
     }
 
+    /**
+     * @param \XF\Mvc\Entity\ArrayCollection|\XF\Mvc\Entity\Entity[] $content
+     * @param $contentType
+     * @param string $countKey
+     * @param string $relationKey
+     *
+     * @return mixed
+     */
     public function addAttachmentsToContent($content, $contentType, $countKey = 'attach_count', $relationKey = 'Attachments')
     {
         $ids = [];
-        foreach ($content AS $id => $item)
+        foreach ($content AS $item)
         {
             if ($item->{$countKey})
             {
-                $ids[] = $id;
+                $ids[] = $item->getEntityId();
             }
         }
 
@@ -184,10 +192,15 @@
                 ->fetch()
                 ->groupBy('content_id');
 
-            foreach ($ids AS $id)
+            foreach ($content AS $item)
             {
-                $contentAttachments = isset($attachments[$id]) ? $attachments[$id] : [];
-                $content[$id]->hydrateRelation($relationKey, $this->em->getBasicCollection($contentAttachments));
+                $contentId = $item->getEntityId();
+
+                $contentAttachments = isset($attachments[$contentId])
+                    ? $this->em->getBasicCollection($attachments[$contentId])
+                    : $this->em->getEmptyCollection();
+
+                $item->hydrateRelation($relationKey, $contentAttachments);
             }
         }
 
Top