Drupal 8 : Entity Query and DB (Database) Query

Entity Query

In Drupal entityQuery allows Web developers to query Drupal entities and its fields in a SQL-like way. A Drupal site's tables and fields are never a 1-to-1 match for the site's entities and fields - entityQuery allows us to query the database as if they were 1-to-1 ways.

Much like you could write some SQL that returns all rows of table1 where field1 is 14, using entityQuery you can ask Drupal to return all entities of type "basic page" where the value of field_some_field is 14. 
Developers can write an entityQuery using the following method to return the QueryInterface:

$query = \Drupal::entityQuery(string $entity_type_id);

Example 1: Simple node entity queries 

This first example returns all nodes of type page where the value of field_some_field (an integer field) is 14.

$query = \Drupal::entityQuery('node')
  ->condition('type', 'page')
  ->condition('field_some_field', 14);
$results = $query->execute();

This example shows that an entityQuery is built up by adding various conditions, sorts, ranges, and other qualifiers. Note that the $query methods are all chainable - each one returns the $query, so we can add multiple conditions per line. 

For conditions, the default operator is "=", so in both of the conditions shown above, the EntityQuery is looking for type=page and field_some_field=14. If we wanted to find all basic page nodes where field_some_field > 14, then the entityQuery would look like this:

$query = \Drupal::entityQuery('node')
  ->condition('type', 'page')
  ->condition('field_some_field', 14, '>');
$results = $query->execute();

 entityQuery() discovery is the fact that it can be used to query field values of referenced entities.

DB Query

A database query is a request for data from a database. Usually the request is to retrieve data; however, data can also be manipulated using queries. The data can come from one or more tables, or even other queries.
OR

A database query is a way of requesting information from the database. A database query can be either a select query or an action query. A select query is a query for retrieving data, while an action query requests additional actions to be performed on the data, like deletion, insertion, and updating.

For example, a manager can perform a query to select the employees who were hired 5 months ago. The results could be the basis for creating performance evaluations.

Example of a Query
As mentioned a moment ago, when we ask Siri for a joke, we are posing a question or query to the application. While the query behind the scenes is very complex and will search against many sources, the concept can be made quite simple.

A query starts with the keyword SELECT, no matter the system. This tells the database to go pick something; the details are after the SELECT statement. If we want all jokes, we use the asterisk (*), which tells the database to retrieve everything:

SELECT * FROM tblJokes;

SELECT * FROM tblJokes WHERE clean_joke = 1;

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...