ArticlesTable.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace TestApp\Model\Table;
  15. use Cake\ORM\Query\SelectQuery;
  16. use Cake\ORM\Table;
  17. use Cake\Utility\Text;
  18. /**
  19. * Article table class
  20. */
  21. class ArticlesTable extends Table
  22. {
  23. public function initialize(array $config): void
  24. {
  25. $this->belongsTo('Authors');
  26. $this->belongsToMany('Tags');
  27. $this->hasMany('ArticlesTags');
  28. }
  29. /**
  30. * Find published
  31. *
  32. * @param \Cake\ORM\Query\SelectQuery $query The query
  33. * @param array<string, mixed> $options The options
  34. */
  35. public function findPublished($query, ?string $title = null): SelectQuery
  36. {
  37. $query = $query->where([$this->aliasField('published') => 'Y']);
  38. if ($title !== null) {
  39. $query->andWhere([$this->aliasField('title') => $title]);
  40. }
  41. return $query;
  42. }
  43. /**
  44. * Find articles and eager load authors.
  45. *
  46. * @param \Cake\ORM\Query\SelectQuery $query The query
  47. * @param array<string, mixed> $options The options
  48. */
  49. public function findWithAuthors($query, array $options = []): SelectQuery
  50. {
  51. return $query->contain('Authors');
  52. }
  53. /**
  54. * Finder for testing named parameter compatibility
  55. */
  56. public function findTitled(SelectQuery $query, array $options): SelectQuery
  57. {
  58. if (!empty($options['title'])) {
  59. $query->where(['Articles.title' => $options['title']]);
  60. }
  61. return $query;
  62. }
  63. public function findSlugged(SelectQuery $query): SelectQuery
  64. {
  65. return $query->formatResults(function ($results) {
  66. return $results->indexBy(function ($row) {
  67. return Text::slug($row['title']);
  68. });
  69. });
  70. }
  71. /**
  72. * Example public method
  73. */
  74. public function doSomething(): void
  75. {
  76. }
  77. /**
  78. * Example Secondary public method
  79. */
  80. public function doSomethingElse(): void
  81. {
  82. }
  83. /**
  84. * Example protected method
  85. */
  86. protected function _innerMethod(): void
  87. {
  88. }
  89. }