示例#1
0
 /**
  * Sample UI to update a record.
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     // Wrap the form in a div.
     $form = array('#prefix' => '<div id="updateform">', '#suffix' => '</div>');
     // Add some explanatory text to the form.
     $form['message'] = array('#markup' => $this->t('Demonstrates a database update operation.'));
     // Query for items to display.
     $entries = DBTNGExampleStorage::load();
     // Tell the user if there is nothing to display.
     if (empty($entries)) {
         $form['no_values'] = array('#value' => t('No entries exist in the table dbtng_example table.'));
         return $form;
     }
     $keyed_entries = array();
     foreach ($entries as $entry) {
         $options[$entry->pid] = t('@pid: @name @surname (@age)', array('@pid' => $entry->pid, '@name' => $entry->name, '@surname' => $entry->surname, '@age' => $entry->age));
         $keyed_entries[$entry->pid] = $entry;
     }
     // Grab the pid.
     $pid = $form_state->getValue('pid');
     // Use the pid to set the default entry for updating.
     $default_entry = !empty($pid) ? $keyed_entries[$pid] : $entries[0];
     // Save the entries into the $form_state. We do this so the AJAX callback
     // doesn't need to repeat the query.
     $form_state->setValue('entries', $keyed_entries);
     $form['pid'] = array('#type' => 'select', '#options' => $options, '#title' => t('Choose entry to update'), '#default_value' => $default_entry->pid, '#ajax' => array('wrapper' => 'updateform', 'callback' => array($this, 'updateCallback')));
     $form['name'] = array('#type' => 'textfield', '#title' => t('Updated first name'), '#size' => 15, '#default_value' => $default_entry->name);
     $form['surname'] = array('#type' => 'textfield', '#title' => t('Updated last name'), '#size' => 15, '#default_value' => $default_entry->surname);
     $form['age'] = array('#type' => 'textfield', '#title' => t('Updated age'), '#size' => 4, '#default_value' => $default_entry->age, '#description' => t('Values greater than 127 will cause an exception'));
     $form['submit'] = array('#type' => 'submit', '#value' => t('Update'));
     return $form;
 }
 /**
  * Render a list of entries in the database.
  */
 public function entryList()
 {
     $content = array();
     $content['message'] = array('#markup' => $this->t('Generate a list of all entries in the database. There is no filter in the query.'));
     $rows = array();
     $headers = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age'));
     foreach ($entries = DBTNGExampleStorage::load() as $entry) {
         // Sanitize each entry.
         $rows[] = array_map('Drupal\\Component\\Utility\\SafeMarkup::checkPlain', (array) $entry);
     }
     $content['table'] = array('#type' => 'table', '#header' => $headers, '#rows' => $rows, '#empty' => t('No entries available.'));
     return $content;
 }
 /**
  * Tests several combinations, adding entries, updating and deleting.
  */
 public function testDBTNGExampleStorage()
 {
     // Create a new entry.
     $entry = array('name' => 'James', 'surname' => 'Doe', 'age' => 23);
     DBTNGExampleStorage::insert($entry);
     // Save another entry.
     $entry = array('name' => 'Jane', 'surname' => 'NotDoe', 'age' => 19);
     DBTNGExampleStorage::insert($entry);
     // Verify that 4 records are found in the database.
     $result = DBTNGExampleStorage::load();
     $this->assertEqual(count($result), 4, 'Found a total of four entries in the table after creating two additional entries.');
     // Verify 2 of these records have 'Doe' as surname.
     $result = DBTNGExampleStorage::load(array('surname' => 'Doe'));
     $this->assertEqual(count($result), 2, 'Found two entries in the table with surname = "Doe".');
     // Now find our not-Doe entry.
     $result = DBTNGExampleStorage::load(array('surname' => 'NotDoe'));
     $this->assertEqual(count($result), 1, 'Found one entry in the table with surname "NotDoe');
     // Our NotDoe will be changed to "NowDoe".
     $entry = $result[0];
     $entry->surname = "NowDoe";
     // update() returns the number of entries updated.
     $this->assertNotEqual(DBTNGExampleStorage::update((array) $entry), 0, "NotDoe updated to NowDoe.");
     $result = DBTNGExampleStorage::load(array('surname' => 'NowDoe'));
     $this->assertEqual(count($result), 1, "Found renamed 'NowDoe' surname");
     // Read only John Doe entry.
     $result = DBTNGExampleStorage::load(array('name' => 'John', 'surname' => 'Doe'));
     $this->assertEqual(count($result), 1, 'Found one entry for John Doe.');
     // Get the entry.
     $entry = (array) end($result);
     // Change age to 45.
     $entry['age'] = 45;
     // Update entry in database.
     DBTNGExampleStorage::update((array) $entry);
     // Find entries with age = 45.
     // Read only John Doe entry.
     $result = DBTNGExampleStorage::load(array('surname' => 'NowDoe'));
     $this->assertEqual(count($result), 1, 'Found one entry with surname = Nowdoe.');
     // Verify it is Jane NowDoe.
     $entry = (array) end($result);
     $this->assertEqual($entry['name'], 'Jane', 'The name Jane is found in the entry');
     $this->assertEqual($entry['surname'], 'NowDoe', 'The surname NowDoe is found in the entry');
     // Delete the entry.
     DBTNGExampleStorage::delete($entry);
     // Verify that now there are only 3 records.
     $result = DBTNGExampleStorage::load();
     $this->assertEqual(count($result), 3, 'Found only three records, a record was deleted.');
 }