See this class in action

  1. <?php
  2. /**
  3.  * db controller
  4.  *
  5.  * @author Mark Nielsen
  6.  * @version $Id$
  7.  * @package blocks/helloworld
  8.  */
  9.  
  10. defined('MOODLE_INTERNAL'or die('Direct access to this script is forbidden.');
  11.  
  12. class block_helloworld_controller_db extends mr_controller_block {
  13.     /**
  14.      * Default screen
  15.      *
  16.      * Demo of mr_db_table
  17.      */
  18.     public function view_action({
  19.         $this->print_header();
  20.  
  21.         echo $this->output->heading('Demo of mr_db_table');
  22.         $this->helper->highlight(__CLASS____FUNCTION__);
  23.         echo $this->output->box_start('generalbox boxaligncenter boxwidthnormal');
  24.  
  25.         #### DEMO CODE ####
  26.         // Create a new instance for table 'block_helloworld'
  27.         $table new mr_db_table('block_helloworld');
  28.  
  29.         // Get table columns
  30.         $result $table->get_columns();
  31.         $this->helper->dump($result'get_columns result');
  32.  
  33.         // Get detailed column information
  34.         $result $table->get_metacolumns();
  35.         $this->helper->dump($result'get_metacolumns result');
  36.  
  37.         // Check for existing column
  38.         $result $table->column_exists('foo');
  39.         $this->helper->dump($result'column_exists result');
  40.  
  41.         // Check non-existent column
  42.         $result $table->column_exists('baz');
  43.         $this->helper->dump($result'column_exists result');
  44.  
  45.         // Generates a mr_db_record setup for this table
  46.         $result $table->record();
  47.         $this->helper->dump($result'record result');
  48.  
  49.         // Form handling example:
  50.         // } else if ($data = $mform->get_data()) {
  51.         //     $table = new mr_db_table('tablename');
  52.         //     $table->save($data);
  53.         //     redirect(...);
  54.         // }
  55.  
  56.         // $table has access to a lot of lib/dmllib.php methods
  57.         // The following routes the call to $DB->get_records()
  58.         $result $table->get_records();
  59.         $this->helper->dump($result'get_records result');
  60.         #### DEMO CODE ####
  61.  
  62.         echo $this->output->box_end();
  63.         $this->print_footer();
  64.     }
  65.  
  66.     /**
  67.      * Demo of mr_db_record
  68.      */
  69.     public function record_action({
  70.         global $DB;
  71.  
  72.         $this->print_header();
  73.  
  74.         echo $this->output->heading('Demo of mr_db_record');
  75.         $this->helper->highlight(__CLASS____FUNCTION__);
  76.         echo $this->output->box_start('generalbox boxaligncenter boxwidthnormal');
  77.  
  78.         #### DEMO CODE ####
  79.         // Create a new record
  80.         $record new mr_db_record('block_helloworld');
  81.  
  82.         echo $this->output->heading('Insert demo');
  83.  
  84.         // Example: direct access
  85.         $record new mr_db_record('block_helloworld');
  86.         $record->name "It's";
  87.         $record->foo  'a';
  88.         $record->bar  'boy';
  89.         $this->helper->dump($record'It\'s a boy result');  // Data is set
  90.  
  91.         // Example: Use set() and save()
  92.         $record new mr_db_record('block_helloworld');
  93.         $data array(
  94.             'name' => "It's",
  95.             'foo'  => 'a',
  96.             'bar'  => 'boy',
  97.             'ding' => 'pow'// This will be ignored by set() because it's an nonexistent field
  98.         );
  99.         $record->set($data)->save();
  100.         $this->helper->dump($record'saved record result');  // id will be set
  101.  
  102.         // You can iterate a record! Woaaaaah!
  103.         echo $this->output->heading('Iteration demo');
  104.         foreach ($record as $key => $value{
  105.             print_object("$key = $value");
  106.         }
  107.  
  108.         // You can count!
  109.         echo $this->output->heading('Count demo');
  110.         $this->helper->dump(count($record)'count result');
  111.  
  112.         // You can access it like an array!
  113.         echo $this->output->heading('Array access demo');
  114.         $this->helper->dump($record['name']'array access result');
  115.  
  116.         // Example: working with an existing record
  117.         $default $DB->get_record('block_helloworld'array('id' => $record->id));
  118.         $record  new mr_db_record('block_helloworld'$default);
  119.         // Do something to change the record otherwise save() will not do anything (which is good!)
  120.         $record->name 'Dingo';
  121.         $record->save();
  122.  
  123.         $result $DB->get_record('block_helloworld'array('id' => $record->id));
  124.         $this->helper->dump($result'save result name = \'Dingo\'');
  125.  
  126.         // Example: delete a record
  127.         $default $DB->get_record('block_helloworld'array('id' => $record->id));
  128.         $record  new mr_db_record('block_helloworld'$default);
  129.         $oldid   $record->id;
  130.         // Something happens and now you have to delete...
  131.         $record->delete();
  132.  
  133.         // Let's see what's in the record now (should be empty)
  134.         $this->helper->dump($record'how record looks after delete()');
  135.  
  136.         // See if the record really was deleted
  137.         $result $DB->get_record('block_helloworld'array('id' => $oldid));
  138.         $this->helper->dump($result'record no longer exists');
  139.         #### DEMO CODE ####
  140.  
  141.         echo $this->output->box_end();
  142.         $this->print_footer();
  143.     }
  144.  
  145.     /**
  146.      * Demo of mr_db_queue
  147.      */
  148.     public function queue_action({
  149.         global $DB;
  150.  
  151.         $this->print_header();
  152.  
  153.         echo $this->output->heading('Demo of mr_db_queue');
  154.         $this->helper->highlight(__CLASS____FUNCTION__);
  155.         echo $this->output->box_start('generalbox boxaligncenter boxwidthnormal');
  156.  
  157.         #### DEMO CODE ####
  158.         // Setup a new queue
  159.         $queue new mr_db_queue();
  160.  
  161.         // Load up the queue with inserts and deletes
  162.         for ($i 0$i 5$i++{
  163.             $record new mr_db_record('block_helloworld');
  164.             $data array(
  165.                 'name' => "It's",  // DO NOT SLASH IT!
  166.                 'foo'  => 'a',
  167.                 'bar'  => 'boy',
  168.             );
  169.             $record->set($data);
  170.             $queue->add($record)// Could also add an array of records!
  171.         }
  172.         if ($records $DB->get_records('block_helloworld')) {
  173.             foreach ($records as $record{
  174.                 $record new mr_db_record('block_helloworld'$record);
  175.                 $record->queue_delete();
  176.                 $queue->add($record);
  177.             }
  178.         }
  179.         // Show whats in the queue
  180.         $this->helper->dump($queue'The loaded queue');
  181.  
  182.         // Flush the queue, should do everything in 2 transactions
  183.         $queue->flush();
  184.  
  185.         // Show whats in the queue after flush
  186.         $this->helper->dump($queue'The flushed queue');
  187.         #### DEMO CODE ####
  188.  
  189.         echo $this->output->box_end();
  190.         $this->print_footer();
  191.     }
  192. }

Documentation generated on Thu, 28 Jun 2012 16:33:51 -0700 by phpDocumentor 1.4.3