Quellcode durchsuchen

Add loadByIds() to Post\Manager

Visman vor 5 Jahren
Ursprung
Commit
702a925ae4
2 geänderte Dateien mit 81 neuen und 1 gelöschten Zeilen
  1. 49 1
      app/Models/Post/Load.php
  2. 32 0
      app/Models/Post/Manager.php

+ 49 - 1
app/Models/Post/Load.php

@@ -6,6 +6,7 @@ use ForkBB\Models\Action;
 use ForkBB\Models\Post\Model as Post;
 use ForkBB\Models\Topic\Model as Topic;
 use InvalidArgumentException;
+use RuntimeException;
 
 class Load extends Action
 {
@@ -60,7 +61,7 @@ class Load extends Action
         $post  = $this->manager->create($data);
         $topic = $post->parent;
 
-        if (! $topic instanceof Topic || $topic->moved_to || ! $topic->parent) {
+        if (! $topic instanceof Topic) {
             return null;
         }
         if (null !== $tid && $topic->id !== $tid) {
@@ -69,4 +70,51 @@ class Load extends Action
 
         return $post;
     }
+
+    /**
+     * Загружает список сообщений из БД
+     *
+     * @throws InvalidArgumentException
+     * @throws RuntimeException
+     */
+    public function loadByIds(array $ids, bool $withTopics): array
+    {
+        foreach ($ids as $id) {
+            if (! \is_int($id) || $id < 1) {
+                throw new InvalidArgumentException('Expected a positive topic id');
+            }
+        }
+
+        $vars = [
+            ':ids' => $ids,
+        ];
+        $sql  = $this->getSql('p.id IN (?ai:ids)');
+        $stmt = $this->c->DB->query($sql, $vars);
+
+        $result   = [];
+        $topicIds = [];
+        while ($row = $stmt->fetch()) {
+            $post = $this->manager->create($row);
+            $topicIds[$post->topic_id] = $post->topic_id;
+            $result[] = $post;
+        }
+
+        if ($withTopics) {
+            $this->c->topics->loadByIds($topicIds, true);
+            foreach ($result as &$post) {
+                if (! $post->parent instanceof Topic) {
+                    $post = null;
+                }
+            }
+            unset($post);
+        } else {
+            foreach ($topicIds as $id) {
+                if (! $this->c->topics->isset($id) || ! $this->c->topics->get($id) instanceof Topic) {
+                    throw new RuntimeException("Topic number {$id} not loaded");
+                }
+            }
+        }
+
+        return $result;
+    }
 }

+ 32 - 0
app/Models/Post/Manager.php

@@ -44,6 +44,38 @@ class Manager extends ManagerModel
         return $post;
     }
 
+    /**
+     * Получает массив сообщений по ids
+     */
+    public function loadByIds(array $ids, bool $withTopics = true): array
+    {
+        $result = [];
+        $data   = [];
+
+        foreach ($ids as $id) {
+            if ($this->isset($id)) {
+                $result[$id] = $this->get($id);
+            } else {
+                $result[$id] = null;
+                $data[]      = $id;
+                $this->set($id, null);
+            }
+        }
+
+        if (empty($data)) {
+            return $result;
+        }
+
+        foreach ($this->Load->loadByIds($data, $withTopics) as $post) {
+            if ($post instanceof Post) {
+                $result[$post->id] = $post;
+                $this->set($post->id, $post);
+            }
+        }
+
+        return $result;
+    }
+
     /**
      * Обновляет сообщение в БД
      *