PaginatorPostsTable.php 1.7 KB

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