Drupal 9 Module Development - Third Edition by Daniel Sipos & Antonio De Marco
Author:Daniel Sipos & Antonio De Marco [Daniel Sipos]
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2020-08-13T16:00:00+00:00
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 definition by which the responsibility for the instantiation is delegated to the factory mentioned, instead of the container as we've seen before. So, the resulting class does not necessarily need to match the one specified for the class key. However, in this case, the Drupal\Core\Database\Connection is an abstract base class that the resulting service extends. Again, in this case, the arguments are responsible for specifying the type of connection that it has to create. The site-default type is used (MySQL, usually), which means that the resulting service will be an instance of Drupal\Core\Database\Driver\mysql\Connection.
From this connection service, we can then request the relevant object with which we can build queries. So, let's see how these work in practice.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7808)
Grails in Action by Glen Smith Peter Ledbrook(7719)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6828)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6828)
Running Windows Containers on AWS by Marcio Morales(6354)
Kotlin in Action by Dmitry Jemerov(5090)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(5064)
Combating Crime on the Dark Web by Nearchos Nearchou(4640)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4603)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4437)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4330)
The Age of Surveillance Capitalism by Shoshana Zuboff(3979)
Python for Security and Networking - Third Edition by José Manuel Ortega(3890)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3550)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3528)
Learn Wireshark by Lisa Bock(3520)
Mastering Python for Networking and Security by José Manuel Ortega(3376)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3355)
Blockchain Basics by Daniel Drescher(3324)
