Topics.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Topic;
  10. use ForkBB\Models\Manager;
  11. use ForkBB\Models\Topic\Topic;
  12. class Topics extends Manager
  13. {
  14. /**
  15. * Ключ модели для контейнера
  16. * @var string
  17. */
  18. protected $cKey = 'Topics';
  19. /**
  20. * Создает новую модель темы
  21. */
  22. public function create(array $attrs = []): Topic
  23. {
  24. return $this->c->TopicModel->setAttrs($attrs);
  25. }
  26. /**
  27. * Получает тему по id
  28. */
  29. public function load(int $id): ?Topic
  30. {
  31. if ($this->isset($id)) {
  32. return $this->get($id);
  33. } else {
  34. $topic = $this->Load->load($id);
  35. $this->set($id, $topic);
  36. return $topic;
  37. }
  38. }
  39. /**
  40. * Получает массив тем по ids
  41. */
  42. public function loadByIds(array $ids, bool $full = true): array
  43. {
  44. $result = [];
  45. $data = [];
  46. foreach ($ids as $id) {
  47. if ($this->isset($id)) {
  48. $result[$id] = $this->get($id);
  49. } else {
  50. $result[$id] = null;
  51. $data[] = $id;
  52. $this->set($id, null);
  53. }
  54. }
  55. if (empty($data)) {
  56. return $result;
  57. }
  58. foreach ($this->Load->loadByIds($data, $full) as $topic) {
  59. if ($topic instanceof Topic) {
  60. $result[$topic->id] = $topic;
  61. $this->set($topic->id, $topic);
  62. }
  63. }
  64. return $result;
  65. }
  66. /**
  67. * Обновляет тему в БД
  68. */
  69. public function update(Topic $topic): Topic
  70. {
  71. return $this->Save->update($topic);
  72. }
  73. /**
  74. * Добавляет новую тему в БД
  75. */
  76. public function insert(Topic $topic): int
  77. {
  78. $id = $this->Save->insert($topic);
  79. $this->set($id, $topic);
  80. return $id;
  81. }
  82. }