PaginatorPostsTable.php 1.8 KB

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