Working with a Database in Drupal 7 - Lesson 5 - Extenders
Select queries in Drupal support extenders. An extender allows you to add functionality to a SELECT query at runtime. This functionality can either be a new method or can override the behavior of an existing one.
This is achieved using object-oriented programming design patterns. Extenders implement the Decorator Pattern, attaching additional responsibilities to a dynamic object by providing a flexible subclass-like extension of query methods.
Working with a DB in Drupal 7 - Lesson 6 - Changing a Query on the Fly (hook_query_alter)
An important feature of dynamic SELECT queries is that other modules can alter these queries on the fly. This allows modules to insert their own instructions into a query, influencing its behavior or applying changes during execution—such as enforcing node access permissions. There are three components involved in altering queries on the fly: tagging, meta data, and hook_query_alter()
.
Working with a Database in Drupal 7 - Lesson 7 - Processing Query Results (fetch)
A SELECT query will always return a result that contains zero or more rows. There are several ways to process query results, and you can choose the one that best fits your needs.
The most common way is using a foreach()
loop:
Working with Database in Drupal 7 - Lesson 8 - Insert Queries (INSERT INTO)
Insert queries should always use the query builder. Some databases require special handlers for LOB (Large Object, such as text in MySQL) and BLOB (Binary Large Object) fields, so an abstraction layer is necessary for individual DB drivers to implement these handlers.
Insert queries start with the db_insert()
function:
Working with a Database in Drupal 7 - Lesson 9 - UPDATE Requests
Update queries should always use the query builder. Different databases have specific handlers for LOB (Large Object, such as TEXT
in MySQL) and BLOB (Binary Large Object) fields, so an abstraction layer is required for individual database drivers to implement these specifics.
Update queries must start with the db_update()
function:
Working with Database in Drupal 7 - Lesson 10 - Delete Requests (DELETE)
Delete queries should use the query builder. They begin with the db_delete()
function:
<?php $query = db_delete('node', $options); ?>
This delete query will remove records from the node
table. Note that you do not need to wrap the table name in curly braces—Drupal's query builder handles that automatically. Delete queries use a Fluent API, meaning all methods (except execute()
) return the query object itself, just like update and insert queries.
Working with Database in Drupal 7 - Lesson 11 - MERGE Queries
Merge queries are a special hybrid type of database query. While the syntax for these queries was defined in SQL 2003, few databases actually support it. However, most databases offer an alternative implementation using specific syntax. The merge query builder in Drupal abstracts the concept of a merge query into an object structure, so it can be compiled appropriately for each database based on its own syntax.
Working with a Database in Drupal 7 - Lesson 12 - Query Conditions (WHERE, HAVING, LIKE)
The WHERE clause in a query allows you to select only those records that meet certain conditions—for example, nodes created no earlier than two weeks ago, or taxonomy terms containing the word “Drupal”. In SQL, we use WHERE and HAVING to specify conditions in SELECT, UPDATE, and DELETE queries. In Drupal’s dynamic queries, there's a built-in mechanism for handling query conditions, and it works the same across SELECT, UPDATE, and DELETE operations.
Conditional Expressions Concept
Conditions are represented as special expressions that define constraints.
Creating a module on Drupal, quick start
Let's start with api.drupal.org. Open the page for the hook_node_validate() hook, which is triggered before a node is saved:
http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_validate/7
Hooks allow us to inject our custom code into the normal Drupal process—adding validations, data fields, form elements, etc.
What does a Drupal 7 module consist of?
Before we start creating our module, I want to share a bit more about the capabilities of the Drupal API. The API provides powerful tools for working with taxonomy, nodes, users, and database input/output. To allow communication between modules and the Drupal core—or between different modules—Drupal provides a hook system. A hook is a callback: when code execution reaches a hook, our module’s function is included in the execution flow. This allows us to process user data, menus, taxonomy, and content types.
Visit: