/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Gather the current user so the new record has ownership. $account = $this->currentUser(); // Save the submitted entry. $entry = array('pid' => $form_state->getValue('pid'), 'name' => $form_state->getValue('name'), 'surname' => $form_state->getValue('surname'), 'age' => $form_state->getValue('age'), 'uid' => $account->id()); $count = DBTNGExampleStorage::update($entry); drupal_set_message(t('Updated entry @entry (@count row updated)', array('@count' => $count, '@entry' => print_r($entry, TRUE)))); }
/** * 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.'); }