$npx -y skills add edutrul/drupal-ai --skill drupal-queriesDrupal database queries — Select, Insert, Update, Delete using the database abstraction layer. Never concatenate SQL.
| 1 | # Drupal Database Queries |
| 2 | |
| 3 | ## CRITICAL: Never Concatenate SQL |
| 4 | |
| 5 | ```php |
| 6 | // WRONG — SQL injection risk |
| 7 | $result = $this->database->query("SELECT * FROM {node} WHERE type = '$type'"); |
| 8 | |
| 9 | // CORRECT — parameterized query |
| 10 | $result = $this->database->query( |
| 11 | "SELECT nid, title FROM {node} WHERE type = :type", |
| 12 | [':type' => $type] |
| 13 | ); |
| 14 | ``` |
| 15 | |
| 16 | ## Select Query |
| 17 | |
| 18 | ```php |
| 19 | $query = $this->database->select('node_field_data', 'n'); |
| 20 | $query->fields('n', ['nid', 'title', 'status']); |
| 21 | $query->condition('n.type', 'article'); |
| 22 | $query->condition('n.status', 1); |
| 23 | $query->orderBy('n.created', 'DESC'); |
| 24 | $query->range(0, 10); |
| 25 | |
| 26 | $results = $query->execute()->fetchAll(); |
| 27 | |
| 28 | // Fetch as associative array |
| 29 | $results = $query->execute()->fetchAllAssoc('nid'); |
| 30 | |
| 31 | // Fetch single value |
| 32 | $count = $query->countQuery()->execute()->fetchField(); |
| 33 | ``` |
| 34 | |
| 35 | ## Select with Join |
| 36 | |
| 37 | ```php |
| 38 | $query = $this->database->select('node_field_data', 'n'); |
| 39 | $query->join('node__field_tags', 'tags', 'n.nid = tags.entity_id'); |
| 40 | $query->fields('n', ['nid', 'title']); |
| 41 | $query->condition('tags.field_tags_target_id', $tid); |
| 42 | $query->condition('n.status', 1); |
| 43 | ``` |
| 44 | |
| 45 | ## Insert |
| 46 | |
| 47 | ```php |
| 48 | $this->database->insert('my_table') |
| 49 | ->fields([ |
| 50 | 'uid' => $uid, |
| 51 | 'data' => serialize($data), |
| 52 | 'created' => \Drupal::time()->getRequestTime(), |
| 53 | ]) |
| 54 | ->execute(); |
| 55 | ``` |
| 56 | |
| 57 | ## Upsert (Insert or Update) |
| 58 | |
| 59 | ```php |
| 60 | $this->database->upsert('my_table') |
| 61 | ->key('uid') |
| 62 | ->fields(['uid', 'data', 'updated']) |
| 63 | ->values([ |
| 64 | 'uid' => $uid, |
| 65 | 'data' => serialize($data), |
| 66 | 'updated' => \Drupal::time()->getRequestTime(), |
| 67 | ]) |
| 68 | ->execute(); |
| 69 | ``` |
| 70 | |
| 71 | ## Update |
| 72 | |
| 73 | ```php |
| 74 | $this->database->update('my_table') |
| 75 | ->fields(['data' => serialize($data)]) |
| 76 | ->condition('uid', $uid) |
| 77 | ->execute(); |
| 78 | ``` |
| 79 | |
| 80 | ## Delete |
| 81 | |
| 82 | ```php |
| 83 | $this->database->delete('my_table') |
| 84 | ->condition('uid', $uid) |
| 85 | ->execute(); |
| 86 | ``` |
| 87 | |
| 88 | ## Entity Query (preferred for entities) |
| 89 | |
| 90 | ```php |
| 91 | // Always prefer EntityQuery over raw SQL for entities |
| 92 | $query = $this->entityTypeManager->getStorage('node')->getQuery() |
| 93 | ->accessCheck(TRUE) |
| 94 | ->condition('type', 'article') |
| 95 | ->condition('status', 1) |
| 96 | ->sort('created', 'DESC') |
| 97 | ->range(0, 10); |
| 98 | |
| 99 | $nids = $query->execute(); |
| 100 | ``` |
| 101 | |
| 102 | ## Inject Database Service |
| 103 | |
| 104 | ```php |
| 105 | use Drupal\Core\Database\Connection; |
| 106 | |
| 107 | public function __construct( |
| 108 | private readonly Connection $database, |
| 109 | ) {} |
| 110 | ``` |
| 111 | |
| 112 | Service ID: `database` |
| 113 | |
| 114 | ## Transactions |
| 115 | |
| 116 | ```php |
| 117 | $transaction = $this->database->startTransaction(); |
| 118 | try { |
| 119 | $this->database->insert('my_table')->fields([...])->execute(); |
| 120 | $this->database->update('other_table')->fields([...])->execute(); |
| 121 | } |
| 122 | catch (\Exception $e) { |
| 123 | $transaction->rollBack(); |
| 124 | throw $e; |
| 125 | } |
| 126 | // Transaction commits when $transaction goes out of scope |
| 127 | ``` |