Running queries
Now that we have some tables to work with, let's take a look at how we can run queries against them. If you are following along, for testing purposes, feel free to add some dummy data into the tables via the database management tool of your choice. We will look at INSERT
statements soon, but before that, we will need to talk about the most common types of query you'll run—SELECT
.
Queries using the Drupal database abstraction layer are run using a central database connection service—database
. Statically, this can be accessed via a shortcut:
$database = \Drupal::database();
This service is a special one compared to the ones we saw before, because it is actually created using a factory. This is its definition to better help you understand what I mean:
database: class: Drupal\Core\Database\Connection factory: Drupal\Core\Database\Database::getConnection arguments: [default]
This is a...