Skip to content

Commit a321645

Browse files
medionpaul-m
medion
authored andcommitted
Issue #2634356 by Mile23, id.medion: Replace deprecated db calls with injected connection in dbtng_example
1 parent 15ffc62 commit a321645

9 files changed

+318
-188
lines changed

dbtng_example/dbtng_example.install

+17-18
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515
* @ingroup dbtng_example
1616
*/
1717
function dbtng_example_install() {
18-
// Add a default entry.
19-
$fields = [
20-
'name' => 'John',
21-
'surname' => 'Doe',
22-
'age' => 0,
18+
// Insert some example data into our schema.
19+
$entries = [
20+
[
21+
'name' => 'John',
22+
'surname' => 'Doe',
23+
'age' => 0,
24+
],
25+
[
26+
'name' => 'John',
27+
'surname' => 'Roe',
28+
'age' => 100,
29+
'uid' => 1,
30+
],
2331
];
24-
db_insert('dbtng_example')
25-
->fields($fields)
26-
->execute();
2732

28-
// Add another entry.
29-
$fields = [
30-
'name' => 'John',
31-
'surname' => 'Roe',
32-
'age' => 100,
33-
'uid' => 1,
34-
];
35-
db_insert('dbtng_example')
36-
->fields($fields)
37-
->execute();
33+
$connection = \Drupal::database();
34+
foreach ($entries as $entry) {
35+
$connection->insert('dbtng_example')->fields($entry)->execute();
36+
}
3837
}
3938

4039
/**

dbtng_example/dbtng_example.module

+12-41
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
* @file
55
* This is an example outlining how a module can use the DBTNG database API.
66
*
7-
* @todo Demonstrate transaction usage.
8-
* https://www.drupal.org/project/examples/issues/2986423
9-
*
10-
* General documentation is available at
11-
* @link database Database abstraction layer documentation @endlink and
12-
* at @link http://drupal.org/node/310069 @endlink.
7+
* @todo Demonstrate more things.
8+
* https://www.drupal.org/project/examples/issues/718672
139
*/
1410

1511
/**
@@ -20,43 +16,18 @@
2016
*
2117
* 'DBTNG' means 'Database: The Next Generation.' Yes, Drupallers are nerds.
2218
*
23-
* General documentation is available at
24-
* @link database.inc database abstraction layer documentation @endlink and
25-
* at @link http://drupal.org/node/310069 Database API @endlink.
26-
*
27-
* The several examples in DbtngExampleController (see
28-
* /src/Drupal/dbtng_example/Controller/DbtngExampleController.php) demonstrate
29-
* basic database usage.
30-
*
31-
* db_insert() example:
32-
* @code
33-
* // INSERT INTO {dbtng_example} (name, surname) VALUES('John, 'Doe')
34-
* db_insert('dbtng_example')
35-
* ->fields(array('name' => 'John', 'surname' => 'Doe'))
36-
* ->execute();
37-
* @endcode
38-
*
39-
* db_update() example:
40-
* @code
41-
* // UPDATE {dbtng_example} SET name = 'Jane' WHERE name = 'John'
42-
* db_update('dbtng_example')
43-
* ->fields(array('name' => 'Jane'))
44-
* ->condition('name', 'John')
45-
* ->execute();
46-
* @endcode
19+
* The interesting database queries for this example module are located in the
20+
* \Drupal\dbtng_example\DbtngExampleRepository class.
4721
*
48-
* db_delete() example:
49-
* @code
50-
* // DELETE FROM {dbtng_example} WHERE name = 'Jane'
51-
* db_delete('dbtng_example')
52-
* ->condition('name', 'Jane')
53-
* ->execute();
54-
* @endcode
22+
* General documentation is available at @link
23+
* https://www.drupal.org/docs/8/api/database-api Database API @endlink.
5524
*
56-
* See @link database Database Abstraction Layer @endlink
57-
* @see db_insert()
58-
* @see db_update()
59-
* @see db_delete()
25+
* @see database
26+
* @see \Drupal\dbtng_example\DbtngExampleRepository
27+
* @see \Drupal\Core\Database\Connection:delete()
28+
* @see \Drupal\Core\Database\Connection:insert()
29+
* @see \Drupal\Core\Database\Connection:select()
30+
* @see \Drupal\Core\Database\Connection:update()
6031
*/
6132

6233
/**
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This declares the plugin manager to the service container. For background
2+
# information on the service container, see https://www.drupal.org/node/2133171.
3+
# Changes here require that the cache be cleared in order to have Drupal notice
4+
# them.
5+
services:
6+
dbtng_example.repository:
7+
class: Drupal\dbtng_example\DbtngExampleRepository
8+
arguments: ['@database', '@string_translation', '@messenger']

dbtng_example/src/Controller/DbtngExampleController.php

+32-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,42 @@
33
namespace Drupal\dbtng_example\Controller;
44

55
use Drupal\Core\Controller\ControllerBase;
6-
use Drupal\dbtng_example\DbtngExampleStorage;
6+
use Drupal\dbtng_example\DbtngExampleRepository;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
78

89
/**
910
* Controller for DBTNG Example.
11+
*
12+
* @ingroup dbtng_example
1013
*/
1114
class DbtngExampleController extends ControllerBase {
1215

16+
/**
17+
* The repository for our specialized queries.
18+
*
19+
* @var \Drupal\dbtng_example\DbtngExampleRepository
20+
*/
21+
protected $repository;
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public static function create(ContainerInterface $container) {
27+
$controller = new static($container->get('dbtng_example.repository'));
28+
$controller->setStringTranslation($container->get('string_translation'));
29+
return $controller;
30+
}
31+
32+
/**
33+
* Construct a new controller.
34+
*
35+
* @param \Drupal\dbtng_example\DbtngExampleRepository $repository
36+
* The repository service.
37+
*/
38+
public function __construct(DbtngExampleRepository $repository) {
39+
$this->repository = $repository;
40+
}
41+
1342
/**
1443
* Render a list of entries in the database.
1544
*/
@@ -29,7 +58,7 @@ public function entryList() {
2958
$this->t('Age'),
3059
];
3160

32-
foreach ($entries = DbtngExampleStorage::load() as $entry) {
61+
foreach ($entries = $this->repository->load() as $entry) {
3362
// Sanitize each entry.
3463
$rows[] = array_map('Drupal\Component\Utility\SafeMarkup::checkPlain', (array) $entry);
3564
}
@@ -64,7 +93,7 @@ public function entryAdvancedList() {
6493
];
6594

6695
$rows = [];
67-
foreach ($entries = DbtngExampleStorage::advancedLoad() as $entry) {
96+
foreach ($entries = $this->repository->advancedLoad() as $entry) {
6897
// Sanitize each entry.
6998
$rows[] = array_map('Drupal\Component\Utility\SafeMarkup::checkPlain', $entry);
7099
}

0 commit comments

Comments
 (0)