PaginatorPostsTable.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://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 http://cakephp.org CakePHP Project
  11. * @since 3.0.0
  12. * @license http://www.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. use Cake\Utility\Hash;
  18. /**
  19. * PaginatorPostsTable class
  20. */
  21. class PaginatorPostsTable extends Table
  22. {
  23. /**
  24. * initialize method
  25. *
  26. * @return void
  27. */
  28. public function initialize(array $config)
  29. {
  30. $this->table('posts');
  31. $this->belongsTo('PaginatorAuthor', [
  32. 'foreignKey' => 'author_id'
  33. ]);
  34. }
  35. /**
  36. * Finder method for find('popular');
  37. */
  38. public function findPopular(Query $query, array $options)
  39. {
  40. $field = $this->alias() . '.' . $this->primaryKey();
  41. $query->where([$field . ' >' => '1']);
  42. return $query;
  43. }
  44. /**
  45. * Finder for published posts.
  46. */
  47. public function findPublished(Query $query, array $options)
  48. {
  49. $query->where(['published' => 'Y']);
  50. return $query;
  51. }
  52. /**
  53. * Custom finder, used with fixture data to ensure Paginator is sending options
  54. *
  55. * @param \Cake\ORM\Query $query
  56. * @param array $options
  57. * @return \Cake\ORM\Query
  58. */
  59. public function findAuthor(Query $query, array $options = [])
  60. {
  61. if (isset($options['author_id'])) {
  62. $query->where(['PaginatorPosts.author_id' => $options['author_id']]);
  63. }
  64. return $query;
  65. }
  66. }