Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
db_select() and similar functions are wrappers on connection object methods and they are deprecated.
In most classes, we should use dependency injection and the database connection object instead of these wrappers.
Solution
Make example how to inject the database connection into service from the container and call crud methods from the Base Database API \Drupal\Core\Database\Connection class.
Comment | File | Size | Author |
---|---|---|---|
#6 | interdiff.txt | 13.84 KB | Mile23 |
#6 | 2634356_6.patch | 28.16 KB | Mile23 |
| |||
#5 | 2634356_5.patch | 22 KB | Mile23 |
| |||
#2 | dbtng_example-add_dependency_injection-2634356-2.patch | 44.98 KB | id.medion |
|
Comments
Comment #2
id.medionI had prepared the patch to include DI into current example, its first functional version, also need some better docs.
Git repository: https://github.com/medion/dbtng_example
Does we need both examples in one module? :
1) with db_select and similar functions
2) with DI implementation
Let's please discuss this.
Comment #3
Mile23OK, so this is good work, and thank you.
However, this issue is a few different topics at the same time.
We have:
The DBTNG example is supposed to demonstrate doing a few things with the DBTNG API, not how or why you should create a new service. In fact, it's almost fair to say it's improperly named, since DBTNG as an API is almost completely deprecated.
So let's work on one thing at a time. I suggest dialing back a little bit and let's just do the proper injection of the connection.
We can create follow-up issues for the other tasks listed above.
Note that there's already a services issue in the works: #2587389: Create service_example
Thanks again.
Comment #4
Mile23See: #2848161: [meta] Replace calls to deprecated db_*() wrappers and #2319859: [META] Deprecate contents of database.inc
Comment #5
Mile23The patch in #2 doesn't apply any more, so that's a good excuse to re-do the work.
This is the same basic idea as @id.medion's changes, but with smaller changes.
DbtngExampleStorage is now called DbtngExampleRepository and is a service used by the forms and controller. This allows for proper injection of the database service, and lets us talk about the repository pattern a little bit.
We can't really enforce this deprecation without making a service, so thanks @id.medion. :-)
Comment #6
Mile23Correcting some docs, adding @ingroup to things... Moved the repository tests to a kernel test.
Comment #8
Mile23Done. :-)