drupal-queries
Skill71 starsChanged 7 months agoMITView on GitHub
Drupal database queries — Select, Insert, Update, Delete using the database abstraction layer. Never concatenate SQL.
In this repository35 other files it gives its agents.
- ddev-expertSkill
- docker-localSkill
- drupal-accessSkill
- drupal-cachingSkill
- drupal-composerSkill
- drupal-configSkill
- drupal-contrib-mgmtSkill
- drupal-debuggingSkill
- drupal-drushSkill
- drupal-dttSkill
- drupal-entity-apiSkill
- drupal-eventsSkill
- drupal-fieldsSkill
- drupal-form-ajaxSkill
- drupal-form-alterSkill
- drupal-form-apiSkill
- drupal-form-validationSkill
- drupal-hooksSkill
- drupal-htmxSkill
- drupal-javascriptSkill
- drupal-kernelSkill
- drupal-menusSkill
- drupal-migrationsSkill
- drupal-paragraphsSkill
- drupal-pluginsSkill
- drupal-renderSkill
- drupal-routesSkill
- drupal-search-apiSkill
- drupal-securitySkill
- drupal-servicesSkill
- drupal-stateSkill
- drupal-taxonomySkill
- drupal-twigSkill
- drupal-unitSkill
- SKILL.mdSkill
The file
Files
Drupal Database Queries
CRITICAL: Never Concatenate SQL
// WRONG — SQL injection risk
$result = $this->database->query("SELECT * FROM {node} WHERE type = '$type'");
// CORRECT — parameterized query
$result = $this->database->query(
"SELECT nid, title FROM {node} WHERE type = :type",
[':type' => $type]
);Select Query
$query = $this->database->select('node_field_data', 'n');
$query->fields('n', ['nid', 'title', 'status']);
$query->condition('n.type', 'article');
$query->condition('n.status', 1);
$query->orderBy('n.created', 'DESC');
$query->range(0, 10);
$results = $query->execute()->fetchAll();
// Fetch as associative array
$results = $query->execute()->fetchAllAssoc('nid');
// Fetch single value
$count = $query->countQuery()->execute()->fetchField();Select with Join
$query = $this->database->select('node_field_data', 'n');
$query->join('node__field_tags', 'tags', 'n.nid = tags.entity_id');
$query->fields('n', ['nid', 'title']);
$query->condition('tags.field_tags_target_id', $tid);
$query->condition('n.status', 1);Insert
$this->database->insert('my_table')
->fields([
'uid' => $uid,
'data' => serialize($data),
'created' => \Drupal::time()->getRequestTime(),
])
->execute();Upsert (Insert or Update)
$this->database->upsert('my_table')
->key('uid')
->fields(['uid', 'data', 'updated'])
->values([
'uid' => $uid,
'data' => serialize($data),
'updated' => \Drupal::time()->getRequestTime(),
])
->execute();Update
$this->database->update('my_table')
->fields(['data' => serialize($data)])
->condition('uid', $uid)
->execute();Delete
$this->database->delete('my_table')
->condition('uid', $uid)
->execute();Entity Query (preferred for entities)
// Always prefer EntityQuery over raw SQL for entities
$query = $this->entityTypeManager->getStorage('node')->getQuery()
->accessCheck(TRUE)
->condition('type', 'article')
->condition('status', 1)
->sort('created', 'DESC')
->range(0, 10);
$nids = $query->execute();Inject Database Service
use Drupal\Core\Database\Connection;
public function __construct(
private readonly Connection $database,
) {}Service ID: database
Transactions
$transaction = $this->database->startTransaction();
try {
$this->database->insert('my_table')->fields([...])->execute();
$this->database->update('other_table')->fields([...])->execute();
}
catch (\Exception $e) {
$transaction->rollBack();
throw $e;
}
// Transaction commits when $transaction goes out of scopeDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

