WorldController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. //
  3. // Database Mapping Test
  4. //
  5. class WorldController extends AppController {
  6. // Needed to enable JsonView
  7. // http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#enabling-data-views-in-your-application
  8. public $components = array('RequestHandler');
  9. protected function _getQueryCount()
  10. {
  11. // Read number of queries to run from URL parameter
  12. // http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-request-parameters
  13. $query_count = $this->request->query('queries');
  14. $query_count = (int)$query_count;
  15. if ($query_count === 0) {
  16. $query_count = 1;
  17. } elseif ($query_count > 500) {
  18. $query_count = 500;
  19. }
  20. return $query_count;
  21. }
  22. public function index() {
  23. // Create an array with the response string.
  24. $worlds = array();
  25. $query_count = $this->_getQueryCount();
  26. // For each query, store the result set values in the response array
  27. for ($i = 0; $i < $query_count; $i++) {
  28. // Retrieve a model by ID
  29. // http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
  30. $worlds[] = $this->World->find('randomId');
  31. }
  32. # Return json list
  33. $this->set('worlds', $worlds);
  34. // Use the CakePHP JSON View
  35. // http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
  36. $this->set('_serialize', 'worlds');
  37. $this->RequestHandler->renderAs($this, 'json');
  38. }
  39. public function query()
  40. {
  41. $world = $this->World->find('randomId');
  42. $this->set('world', $world);
  43. $this->set('_serialize', 'world');
  44. $this->RequestHandler->renderAs($this, 'json');
  45. }
  46. public function updates()
  47. {
  48. // Create an array with the response string.
  49. $worlds = [];
  50. $query_count = $this->_getQueryCount();
  51. // For each query, store the result set values in the response array
  52. for ($i = 0; $i < $query_count; $i++) {
  53. // Retrieve a model by ID
  54. // http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
  55. $world = $this->World->find('randomId');
  56. $world['randomNumber'] = mt_rand(1, 10000);
  57. $this->World->save(['World' => $world]);
  58. $worlds[] = $world;
  59. }
  60. # Return json list
  61. $this->set('worlds', $worlds);
  62. // Use the CakePHP JSON View
  63. // http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
  64. $this->set('_serialize', 'worlds');
  65. $this->RequestHandler->renderAs($this, 'json');
  66. }
  67. }