12

According to db_insert manual page this function is deprecated and it is better to use Drupal 8 database connection to perform insert.

Deprecated

as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call insert() on it. For example, $injected_database->insert($table, $options);

Now how can I get a database connection and call insert() method?

  • Do you mean outside of a class with injected services? Like \Drupal::database()->insert(...);? – Clive Dec 19 '15 at 0:03
  • No i mean inside class with injected services class PetmdController extends ControllerBase – Mohammad Ali Akbari Dec 19 '15 at 0:22
19

To inject the database service add/alter the following methods in your controller class:

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;

class PetmdController extends ControllerBase {

  protected $database;

  public function __construct(Connection $database) {
    $this->database = $database;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('database')
    );
  }

  public function foo() {
    $this->database->insert(...)->fields(...)->execute();
  }
}
  • This works great for ControllerBase, but how would I inject the database service if my class was extending ContentEntityBase that is already passing different variables into the construct? – Felix Eve Oct 2 '17 at 0:17
3

To add to Berdir answer here is how you can inject your database service in your controller

$db = \Drupal::database();
      $query = $db->select('location','loc');
      $query->fields('loc', array('id', 'name', 'bond_goal','deposit_goal','date_created','date_updated'));
      $query->addField('loc','name','location_title');
      $table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')->orderByHeader($header);
      $pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);

You can study core/lib/Drupal/Core/Database/Query classes for more information

2

First, as your quote says, it is deprecated for Drupal 9. Which means it will stay around for years and will not be removed from Drupal 8 ever.

But yes, its a good idea to avoid deprecated functions. Like any other deprecated function, you can always simply look at its implementation to see how the new way of doing it is. Although instead of calling out to \Drupal, you want to inject the database or whatever other service you need when that is possible (when you are in a service, controller, form, plugin, ...)

1

Option 1:

$db = \Drupal::database();
$query = $db->select('k_product', 'p');
$query->fields('p', ['idpr', 'name', 'type']);
$data = $query->execute()->fetchAllAssoc('idpr', 'name', 'type');

Option 2

$db = \Drupal::database();
$data = $db->query('SELECT idpr, name, code, detail FROM k_product')->fetchAllAssoc('idpr', 'name');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.