SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Models/CategoryModel.php

  1. <?php
  2. namespace Sprout\Models;
  3.  
  4. use DateTimeImmutable;
  5. use Sprout\Helpers\Category;
  6. use karmabunny\kb\Collection;
  7. use karmabunny\pdb\Pdb;
  8. use karmabunny\pdb\PdbQuery;
  9.  
  10.  
  11. class CategoryModel extends Collection
  12. {
  13. /** @var int */
  14. public $id = 0;
  15.  
  16. /** @var string */
  17. public $name;
  18.  
  19. /** @var string */
  20. public $date_added;
  21.  
  22. /** @var string */
  23. public $date_modified;
  24.  
  25.  
  26. /** @inheritdoc */
  27. public static function getConnection(): Pdb
  28. {
  29. return \Sprout\Helpers\Pdb::getInstance();
  30. }
  31.  
  32.  
  33. /**
  34.   *
  35.   * @return DateTimeInterface
  36.   */
  37. public function getDateAdded()
  38. {
  39. return new DateTimeImmutable($this->date_added);
  40. }
  41.  
  42.  
  43. /**
  44.   *
  45.   * @return DateTimeInterface
  46.   */
  47. public function getDateModified()
  48. {
  49. return new DateTimeImmutable($this->date_modified);
  50. }
  51.  
  52.  
  53. /**
  54.   * Create a query for this model.
  55.   *
  56.   * @param array $main_table
  57.   * @param array $conditions
  58.   * @return PdbQuery
  59.   */
  60. public static function find(string $main_table, array $conditions = []): PdbQuery
  61. {
  62. $pdb = static::getConnection();
  63. $cat_table = Category::tableMain2cat($main_table);
  64. $join_table = Category::tableMain2joiner($main_table);
  65. return (new PdbQuery($pdb))
  66. ->join($join_table, ['cat_id = id'])
  67. ->find($cat_table, $conditions)
  68. ->as(static::class);
  69. }
  70.  
  71.  
  72. /**
  73.   * Find one model.
  74.   *
  75.   * @param array $main_table
  76.   * @param array $conditions
  77.   * @return static
  78.   */
  79. public static function findOne(string $main_table, array $conditions)
  80. {
  81. /** @var static */
  82. return self::find($main_table, $conditions)->one();
  83. }
  84.  
  85.  
  86. /**
  87.   * Find a list of models.
  88.   *
  89.   * @param array $main_table
  90.   * @param array $conditions
  91.   * @return static[]
  92.   */
  93. public static function findAll(string $main_table, array $conditions = [])
  94. {
  95. return self::find($main_table, $conditions)->all();
  96. }
  97.  
  98.  
  99. /**
  100.   * Save this model.
  101.   *
  102.   * @param string $main_table
  103.   * @return bool
  104.   * @throws InvalidArgumentException
  105.   * @throws QueryException
  106.   * @throws ConnectionException
  107.   * @throws Exception
  108.   * @throws TransactionRecursionException
  109.   * @throws PDOException
  110.   */
  111. public function save(string $main_table): bool
  112. {
  113. $pdb = static::getConnection();
  114. $cat_table = Category::tableMain2cat($main_table);
  115.  
  116. $now = Pdb::now();
  117. $data = iterator_to_array($this);
  118. $conditions = [ 'id' => $this->id ];
  119.  
  120.  
  121. if ($this->id > 0) {
  122. $data['date_modified'] = $now;
  123.  
  124. $pdb->update($cat_table, $data, $conditions);
  125. }
  126. else {
  127. $data['date_added'] = $now;
  128. $data['date_modified'] = $now;
  129.  
  130. // TODO Add shared transaction support.
  131. $ts_id = 0;
  132. if (!$pdb->inTransaction()) {
  133. $ts_id = 1;
  134. $pdb->transact();
  135. }
  136.  
  137. $this->id = $pdb->insert($cat_table, $data);
  138.  
  139. if ($ts_id === 1) {
  140. $pdb->commit();
  141. }
  142. }
  143.  
  144. $this->date_added = $data['date_added'];
  145. $this->date_modified = $data['date_modified'];
  146.  
  147. return (bool) $this->id;
  148. }
  149.  
  150.  
  151. /**
  152.   * Delete this model.
  153.   *
  154.   * @param string $main_table
  155.   * @return bool
  156.   * @throws InvalidArgumentException
  157.   * @throws QueryException
  158.   * @throws ConnectionException
  159.   */
  160. public function delete(string $main_table): bool
  161. {
  162. $pdb = static::getConnection();
  163. $cat_table = Category::tableMain2cat($main_table);
  164. return (bool) $pdb->delete($cat_table, ['id' => $this->id]);
  165. }
  166. }
  167.