[Drupal-8] How to Handle Select, Update, Delete Database Operations in Drupal 8?
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY akhila.vn
8 months ago
Drupal-8
0 comments comment

In Drupal 8, the usage of database operations such as select, update and delete are slightly changed from Drupal 7. The db_query() is deprecated in Drupal 8. To fetch a field we can use :

$query = \Drupal::database()->query('SELECT myfield from my_table where
  field1 = :value1 AND field2 = :value2', array(
    ':value1' => $condition1,
    ':value2' => $condition2,
  )
);
$data = $query->fetchField();

Now we can checkout the select, update, and delete operations in Drupal 8. For a field selection use,

$query = \Drupal::database()->select('table_name', 'alias')
  ->fields('alias', ['field1', field2])
  ->condition('field3', $condition);
$results = $query->execute();
while ($content = $results->fetchAssoc()) {
  // Operations using $content.
}

For a single field selection we can use $last_paper_id = $last_paper->fetchField();

For update query execution in Drupal 8, we can use,

$table = 'table_name';
\Drupal::database()->update($table)
  ->fields(array('field1' => $value1, 'field2' => $value2))
  ->condition('field3', $condition1)
  ->condition('field4', $condition2)
  ->execute();

For content deletion,

$query = \Drupal::database()->delete('table_name');
  $query->condition('field1', $condition1);
  $query->condition('field2', $condition2);
  $query->execute();

Now checkout other database statements in Drupal 8 from this link!


RELATED ARTICLE

more_horiz
close

on 21st May 2013 / by Anoop John
Drupal 6 will be maintained till the release of Drupal 8. Drupal 8 release is expected to happen towards the end of 2013. The current stable branch Drupal 7 will be maintained till the release of Drupal 9 (probably 2015). If you are running a production site on Drupal 6 then you will probably have to start looking at upgrading the site by the end of this year. Drupal Drupal 6 Drupal Upgrade Drupal 7 Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 26th December 2014 / by abhai.sasidharan
YAML is a data serialization language which is easily readable to both humans and computers. It is for this very reason that YAML is being used in Drupal 8. YAML stands for YAML Ain't Markup Language. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
more_horiz
close

on 29th December 2014 / by abhai.sasidharan
Drupal, up until now a PAC (Presentation Abstraction Control) architecture, has taken a step closer to MVC (Model View Controller) architecture. Although nowhere have they mentioned about MVC, the new Drupal 8 is strictly object oriented. Another feature in Drupal 8, that is different from the existing versions of Drupal is that it is not entirely Drupal. Drupal 8 uses many components in symfony like it's ClassLoader, YAML file structure, twig and so on. For a detailed description please visit the Drupal 8 home page. Drupal Drupal 8 Leave a reply Your email address will not be published. Required fields are marker *
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.