WorldController.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. if ($query_count == null) {
  14. $query_count = 1;
  15. }
  16. // Create an array with the response string.
  17. $arr = array();
  18. // For each query, store the result set values in the response array
  19. for ($i = 0; $i < $query_count; $i++) {
  20. // Choose a random row
  21. // http://www.php.net/mt_rand
  22. $id = mt_rand(1, 10000);
  23. // Retrieve a model by ID
  24. // http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find
  25. $world = $this->World->find('first', array('conditions' => array('id' => $id)));
  26. // Store result in array.
  27. $arr[] = array("id" => $world['World']['id'], "randomNumber" => $world['World']['randomNumber']);
  28. }
  29. // Use the CakePHP JSON View
  30. // http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
  31. $this->set('worlds', $arr);
  32. $this->set('_serialize', array('worlds'));
  33. }
  34. }
  35. ?>