O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Insert queries

In order to get data into our custom database tables, we have an INSERT query builder that we can use. For this, and, the other types of queries, it is highly discouraged to use the db_query() approach because Drupal cannot ensure that it works across the different types of database engines. Instead, we can use the insert() method on the connection service and build our query using the Insert object that gets returned. So, let's see how we can add a record to our players table:

$database->insert('players');$fields = ['name' => 'Diego M', 'data' => serialize(['known for' => 'Hand of God'])];$id = $database->insert('players')  ->fields($fields)  ->execute();

The main thing about an insert query is the fields() method. It expects ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required