PaginatorPostsTable.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright 2005-2011, Cake Software Foundation, Inc.
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
  11. * @link https://cakephp.org CakePHP Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace TestApp\Model\Table;
  16. use Cake\ORM\Query\SelectQuery;
  17. use Cake\ORM\Table;
  18. /**
  19. * PaginatorPostsTable class
  20. */
  21. class PaginatorPostsTable extends Table
  22. {
  23. /**
  24. * initialize method
  25. */
  26. public function initialize(array $config): void
  27. {
  28. $this->setTable('posts');
  29. $this->belongsTo('PaginatorAuthor', [
  30. 'foreignKey' => 'author_id',
  31. ]);
  32. }
  33. /**
  34. * Finder method for find('popular');
  35. */
  36. public function findPopular(SelectQuery $query): Select
  37. {
  38. $field = $this->getAlias() . '.' . $this->getPrimaryKey();
  39. $query->where([$field . ' >' => '1']);
  40. return $query;
  41. }
  42. /**
  43. * Finder for published posts.
  44. */
  45. public function findPublished(SelectQuery $query): SelectQuery
  46. {
  47. $query->where(['published' => 'Y']);
  48. return $query;
  49. }
  50. /**
  51. * Custom finder, used with fixture data to ensure Paginator is sending options
  52. */
  53. public function findAuthor(SelectQuery $query, ?int $authorId = null): SelectQuery
  54. {
  55. if ($authorId) {
  56. $query->where(['PaginatorPosts.author_id' => $authorId]);
  57. }
  58. return $query;
  59. }
  60. }