WorldController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. public function index() {
  10. // Read number of queries to run from URL parameter
  11. // http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-request-parameters
  12. $query_count = $this->request->query('queries');
  13. $should_return_array = True;
  14. if ($query_count == null) {
  15. $query_count = 1;
  16. $should_return_array = False;
  17. }
  18. $query_count = intval($query_count);
  19. if ($query_count == 0) {
  20. $query_count = 1;
  21. } elseif ($query_count > 500) {
  22. $query_count = 500;
  23. }
  24. // Create an array with the response string.
  25. $arr = array();
  26. // For each query, store the result set values in the response array
  27. for ($i = 0; $i < $query_count; $i++) {
  28. // Choose a random row
  29. // http://www.php.net/mt_rand
  30. $id = mt_rand(1, 10000);
  31. // Retrieve a model by ID
  32. // http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
  33. $world = $this->World->find('first', array('conditions' => array('id' => $id)));
  34. // Store result in array.
  35. $arr[] = array("id" => $world['World']['id'], "randomNumber" => $world['World']['randomNumber']);
  36. }
  37. # Return either one object or a json list
  38. if ($should_return_array == False) {
  39. $this->set('worlds', $arr[0]);
  40. } else {
  41. $this->set('worlds', $arr);
  42. }
  43. // Use the CakePHP JSON View
  44. // http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
  45. $this->set('_serialize', 'worlds');
  46. }
  47. }
  48. ?>