Skip to content
Snippets Groups Projects

Issue #2991337: How to get database connection in the code? Which method is recomended?

Closed Issue #2991337: How to get database connection in the code? Which method is recomended?
4 unresolved threads
Closed Oleh Vehera requested to merge issue/drupal-2991337:2991337-how-to-get into 11.x
4 unresolved threads

Merge request reports

Code Quality is loading
Test summary results are being parsed

Closed by Théodore BiadalaThéodore Biadala 9 months ago (Aug 8, 2024 2:13pm UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • 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
  • quietone
  • Oleh Vehera changed target branch from 9.3.x to 11.x

    changed target branch from 9.3.x to 11.x

  • Oleh Vehera added 2584 commits

    added 2584 commits

    Compare with previous version

  • Oleh Vehera
  • Oleh Vehera resolved all threads

    resolved all threads

  • Oleh Vehera added 1 commit

    added 1 commit

    • 1ec6c875 - Apply 3 suggestion(s) to 1 file(s)

    Compare with previous version

  • Oleh Vehera
  • added 314 commits

    Compare with previous version

  • added 1 commit

    Compare with previous version

  • Oleh Vehera resolved all threads

    resolved all threads

  • Oleh Vehera added 56 commits

    added 56 commits

    Compare with previous version

  • Oleh Vehera added 53 commits

    added 53 commits

    Compare with previous version

  • Oleh Vehera added 9 commits

    added 9 commits

    Compare with previous version

  • 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;
  • 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
  • quietone added 1887 commits

    added 1887 commits

    Compare with previous version

  • quietone added 1 commit

    added 1 commit

    Compare with previous version

  • 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) {
  • Stephen Mustgrave
  • quietone added 173 commits

    added 173 commits

    Compare with previous version

  • Please register or sign in to reply
    Loading