__construct()
public function __construct(?string $module_name = null, ?string $db_group = null)
Description
Initializes a database connection using PDO with configuration from config/database.php. Supports multiple database groups and provides environment‑aware error handling (detailed errors in development, generic errors in production). The constructor also blocks direct URL access to the Db module for security.
IMPORTANT: The framework automatically passes the module name as the first parameter when instantiating Db via $this->db. When creating Db instances manually, you can omit the module name.
Parameters
| Parameter | Type | Description | Default | Required |
|---|---|---|---|---|
| module_name | string|null | The module name (passed by framework for integration with Trongate module system). Can be null for manual instantiation. | null | No |
| db_group | string|null | Database group name from config/database.php (defaults to 'default'). | null | No |
Return Value
| Type | Description |
|---|---|
| void | No return value. |
Exceptions
- Exception - If the specified database group is not configured in /config/database.php.
- Exception - If the database connection fails.
Example #1: Framework Instantiation (Typical Usage)
In controllers and models, Db is automatically available via $this->db:
<?php
class Products extends Trongate {
public function index() {
// $this->db is already instantiated by the framework
$products = $this->db->get('id', 'products');
// ...
}
}Example #2: Manual Instantiation with Default Database
Creating a Db instance manually (outside framework context):
// Uses 'default' database group from config/database.php
$db = new Db();
// Now use like any Db instance
$users = $db->get('id', 'users');Example #3: Manual Instantiation with Specific Database Group
Connecting to an alternative database defined in config/database.php:
// Connect to 'analytics' database group
$analytics_db = new Db(null, 'analytics');
// Query the analytics database
$stats = $analytics_db->get('date', 'page_views');Example #4: Framework Instantiation with Custom Database Group
Within a module, you can load Db with a specific database group:
<?php
class Reports extends Trongate {
public function __construct() {
parent::__construct();
// Load Db with 'analytics' database group
$this->module('db', 'analytics');
// Now $this->db points to the analytics database
}
public function index() {
$data = $this->db->get('id', 'analytics_data');
// ...
}
}Configuration Example (config/database.php)
$databases['default'] = [
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'secret',
'database' => 'myapp'
];
$databases['analytics'] = [
'host' => 'analytics.db.example.com',
'port' => '3306',
'user' => 'analytics_user',
'password' => 'analytics_pass',
'database' => 'analytics_db'
];Important Notes
- Always calls
parent::__construct($module_name)first (required by framework). - Blocks URL access to the Db module via
block_url('db')for security. - Determines environment mode (dev/prod) from the
ENVconstant. - Uses persistent PDO connections by default (
PDO::ATTR_PERSISTENT => true). - Sets error mode to
PDO::ERRMODE_EXCEPTION. - If the database name is empty in configuration, returns without connecting (useful for tests).
- In development mode (
ENV === 'dev'), errors include detailed messages. - In production mode, errors are generic to avoid leaking sensitive information.
- The
$module_nameparameter is primarily for framework integration; you can ignore it when creating instances manually.