db_controller.php 962 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. class DbController extends AppController
  3. {
  4. protected function before_filter()
  5. {
  6. View::select(null, null);
  7. header('Content-type: application/json');
  8. }
  9. public function index()
  10. {
  11. echo json_encode(World::byId(mt_rand(1, 10000)));
  12. }
  13. public function queries($count = 1)
  14. {
  15. $count = is_numeric($count) ? min(max($count, 1), 500) : 1;
  16. $worlds = [];
  17. for ($i = 0; $i < $count; ++$i) {
  18. $worlds[] = World::byId(mt_rand(1, 10000));
  19. }
  20. echo json_encode($worlds);
  21. }
  22. public function update($count = 1)
  23. {
  24. $count = is_numeric($count) ? min(max($count, 1), 500) : 1;
  25. $worlds = [];
  26. for ($i = 0; $i < $count; ++$i) {
  27. $row = World::byId(mt_rand(1, 10000));
  28. $row->randomNumber = mt_rand(1, 10000);
  29. $row->update();
  30. $worlds[] = $row;
  31. }
  32. echo json_encode($worlds);
  33. }
  34. }