Skip to content

Instantly share code, notes, and snippets.

@laceysanderson
Last active December 11, 2023 18:02
Show Gist options
  • Save laceysanderson/898e939aff1e5c38452aa1f04990e80c to your computer and use it in GitHub Desktop.
Save laceysanderson/898e939aff1e5c38452aa1f04990e80c to your computer and use it in GitHub Desktop.
Inserting test data vie drush:php-cli
// First get access to the chado database connection.
$chado = \Drupal::service('tripal_chado.database');
// Create a function to make random strings.
$randomString = function($len = 10) {$word = array_merge(range('a', 'z'), range('A', 'Z')); shuffle($word); return substr(implode($word), 0, $len);};
// Insert a set number of organisms
$total = 5;
$subspecies = 2868; // cvterm_id
for ($i = 1; $i <= $total; $i++) {
$values = [
'genus' => 'Tripalus',
'species' => 'databasica',
'type_id' => $subspecies,
'infraspecific_name' => $randomString(25),
'common_name' => 'Tripal ' . $i,
];
$values['abbreviation'] = 'T. databasica ssp. ' . $values['infraspecific_name'];
$chado->insert('1:organism')
->fields($values)
->execute();
}
// Look at them all.
$chado->query("SELECT * FROM {1:organism}")->fetchAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment