Issue #2991337: How to get database connection in the code? Which method is recomended?
4 unresolved threads
Merge request reports
Activity
added 590 commits
-
c0ee7c56...4894881a - 589 commits from branch
project:9.3.x
- f655c94d - Apply #34 patch
-
c0ee7c56...4894881a - 589 commits from branch
- Resolved by Oleh Vehera
225 225 * @endcode 226 226 * if you had a connection object variable $connection available to use. See 227 227 * also the @link container Services and Dependency Injection topic. @endlink 228 * In Object Oriented code: 229 * - If possible, use dependency injection to use @database service or 230 * $container->get('database'); to inject the database connection; 231 * - If it is not possible to use dependency injection, e.g. in a static 232 method, use \Drupal::database(); 233 * @code 234 * $connection = \Drupal::database(); 235 * $query = $connection->query('...'); 236 * @endcode 237 * - If services are not yet available, 238 * \Drupal\Core\Database\Database::getConnection() can get a database changed this line in version 13 of the diff
- Resolved by Oleh Vehera
added 2584 commits
-
9af23ab9...1aaa480b - 2582 commits from branch
project:11.x
- 04bd0820 - Apply #34 patch
- 40e65f2d - Address #42
-
9af23ab9...1aaa480b - 2582 commits from branch
- Resolved by Oleh Vehera
- Resolved by Oleh Vehera
239 239 * @endcode 240 240 * if you had a connection object variable $connection available to use. See 241 241 * also the @link container Services and Dependency Injection topic. @endlink 242 * In Object Oriented code: 243 * - If possible, use dependency injection to use "database" service or 244 * $container->get('database'); to inject the database connection; changed this line in version 13 of the diff
251 * - If services are not yet available, use 252 * \Drupal\Core\Database\Database::getConnection() can get a database 253 * connection; 254 * @code 255 * use Drupal\Core\Database\Database; 256 * 257 * // ... 258 * 259 * $connection = Database::getConnection(); 260 * $query = $connection->query('...'); 261 * @endcode 262 * - In unit tests, we do not have a booted kernel or a built container. A 263 * unit tests which need a database service should be converted to a 264 * kernel test; 265 * - In kernel and functional test classes we have 266 * $this->container->get('database'). Some test authors might discover that This is contradicting https://www.drupal.org/project/drupal/issues/2066993
changed this line in version 14 of the diff
239 239 * @endcode 240 240 * if you had a connection object variable $connection available to use. See 241 241 * also the @link container Services and Dependency Injection topic. @endlink 242 * In Object Oriented code: 243 * - If possible, use dependency injection to use the "database" service. 244 * @code 245 * use Drupal\Core\Database\Connection; 246 * 247 * class myClass { 248 * 249 * protected Connection $database; 250 * 251 * public function __construct(Connection $database) { changed this line in version 15 of the diff
- Resolved by Théodore Biadala
Please register or sign in to reply