Add loadByIds() to Post\Manager

This commit is contained in:
Visman 2020-06-06 22:41:49 +07:00
parent 6e3c28f4f3
commit 702a925ae4
2 changed files with 81 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -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;
}
/**
* Обновляет сообщение в БД
*