AuthorsTable.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @since 3.0.0
  11. * @license https://opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace TestApp\Model\Table;
  14. use Cake\ORM\Query;
  15. use Cake\ORM\Table;
  16. /**
  17. * Author table class
  18. */
  19. class AuthorsTable extends Table
  20. {
  21. public function initialize(array $config)
  22. {
  23. $this->hasMany('articles');
  24. }
  25. public function findByAuthor(Query $query, array $options = [])
  26. {
  27. if (isset($options['author_id'])) {
  28. $query->where(['Articles.id' => $options['author_id']]);
  29. }
  30. return $query;
  31. }
  32. /**
  33. * Finder that applies a formatter to test dirty associations
  34. *
  35. * @param \Cake\ORM\Query $query The query
  36. * @param array $options The options
  37. * @return \Cake\ORM\Query
  38. */
  39. public function findFormatted(Query $query, array $options = [])
  40. {
  41. return $query->formatResults(function ($results) {
  42. return $results->map(function ($author) {
  43. $author->formatted = $author->name . '!!';
  44. return $author;
  45. });
  46. });
  47. }
  48. }