DbConfig

Type
Class
Namespace
craft\config
Inherits
craft\config\DbConfig » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.0.0

DB config class

View source (opens new window)

# Public Properties

Property Description
attributes array (opens new window) – An array of key => value pairs of PDO attributes to pass into the PDO constructor.
charset string (opens new window) – The charset to use when creating tables.
collation string (opens new window), null (opens new window) – The collation to use when creating tables.
database string (opens new window), null (opens new window) – The name of the database to select.
driver string (opens new window), null (opens new window) – The database driver to use.
dsn string (opens new window), null (opens new window) – The Data Source Name (“DSN”) that tells Craft how to connect to the database.
password string (opens new window) – The database password to connect with.
port integer (opens new window), null (opens new window) – The database server port.
schema string (opens new window) – The schema that Postgres is configured to use by default (PostgreSQL only).
server string (opens new window), null (opens new window) – The database server name or IP address.
setSchemaOnConnect boolean (opens new window) – Whether the schema should be explicitly used for database queries (PostgreSQL only).
tablePrefix string (opens new window) – If you’re sharing Craft installs in a single database (MySQL) or a single database and using a shared schema (PostgreSQL), you can set a table prefix here to avoid per-install table naming conflicts.
unixSocket string (opens new window), null (opens new window) – MySQL only.
url string (opens new window), null (opens new window) – The database connection URL, if one was provided by your hosting environment.
useUnbufferedConnections boolean (opens new window) – Whether batched queries should be executed on a separate, unbuffered database connection.
user string (opens new window) – The database username to connect with.

# attributes

Type
array (opens new window)
Default value
[]

An array of key => value pairs of PDO attributes to pass into the PDO constructor.

For example, when using the MySQL PDO driver (opens new window), if you wanted to enable a SSL database connection (assuming SSL is enabled in MySQL (opens new window) and 'user' can connect via SSL, you’d set these:

[
    PDO::MYSQL_ATTR_SSL_KEY    => '/path/to/my/client-key.pem',
    PDO::MYSQL_ATTR_SSL_CERT   => '/path/to/my/client-cert.pem',
    PDO::MYSQL_ATTR_SSL_CA     => '/path/to/my/ca-cert.pem',
],

View source (opens new window)

# charset

Type
string (opens new window)
Default value
'utf8'

The charset to use when creating tables.

TIP

You can change the character set and collation across all existing database tables using this terminal command:

> php craft db/convert-charset

View source (opens new window)

# collation

Type
string (opens new window), null (opens new window)
Default value
null
Since
3.6.4

The collation to use when creating tables.

This is only used by MySQL. If null, the charset’s default collation will be used.

Charset Default collation
utf8 utf8_general_ci
utf8mb4 utf8mb4_0900_ai_ci

TIP

You can change the character set and collation across all existing database tables using this terminal command:

> php craft db/convert-charset

View source (opens new window)

# database

Type
string (opens new window), null (opens new window)
Default value
null

The name of the database to select.

View source (opens new window)

# driver

Type
string (opens new window), null (opens new window)
Default value
null

The database driver to use. Either mysql for MySQL or pgsql for PostgreSQL.

View source (opens new window)

# dsn

Type
string (opens new window), null (opens new window)
Default value
null

The Data Source Name (“DSN”) that tells Craft how to connect to the database.

DSNs should begin with a driver prefix (mysql: or pgsql:), followed by driver-specific parameters. For example, mysql:host=127.0.0.1;port=3306;dbname=acme_corp.

View source (opens new window)

# password

Type
string (opens new window)
Default value
''

The database password to connect with.

View source (opens new window)

# port

Type
integer (opens new window), null (opens new window)
Default value
null

The database server port. Defaults to 3306 for MySQL and 5432 for PostgreSQL.

View source (opens new window)

# schema

Type
string (opens new window)
Default value
'public'

The schema that Postgres is configured to use by default (PostgreSQL only).

TIP

To force Craft to use the specified schema regardless of PostgreSQL’s search_path setting, you must enable the setSchemaOnConnect setting.

See also https://www.postgresql.org/docs/8.2/static/ddl-schemas.html

View source (opens new window)

# server

Type
string (opens new window), null (opens new window)
Default value
null

The database server name or IP address. Usually localhost or 127.0.0.1.

View source (opens new window)

# setSchemaOnConnect

Type
boolean (opens new window)
Default value
false
Since
3.7.27

Whether the schema should be explicitly used for database queries (PostgreSQL only).

WARNING

This will cause an extra SET search_path SQL query to be executed per database connection. Ideally, PostgreSQL’s search_path setting should be configured to prioritize the desired schema.

View source (opens new window)

# tablePrefix

Type
string (opens new window)
Default value
''

If you’re sharing Craft installs in a single database (MySQL) or a single database and using a shared schema (PostgreSQL), you can set a table prefix here to avoid per-install table naming conflicts. This can be no more than 5 characters, and must be all lowercase.

View source (opens new window)

# unixSocket

Type
string (opens new window), null (opens new window)
Default value
null

MySQL only. If this is set, the CLI connection string (used for yiic) will connect to the Unix socket instead of the server and port. If this is specified, then server and port settings are ignored.

View source (opens new window)

# url

Type
string (opens new window), null (opens new window)
Default value
null

The database connection URL, if one was provided by your hosting environment.

If this is set, the values for driver, user, database, server, port, and database will be extracted from it.

View source (opens new window)

# useUnbufferedConnections

Type
boolean (opens new window)
Default value
false
Since
3.7.0

Whether batched queries should be executed on a separate, unbuffered database connection.

This setting only applies to MySQL. It can be enabled when working with high volume content, to prevent PHP from running out of memory when querying too much data at once. (See https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder#batch-query-mysql (opens new window) for an explanation of MySQL’s batch query limitations.)

View source (opens new window)

# user

Type
string (opens new window)
Default value
'root'

The database username to connect with.

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() Initializes the object.
updateDsn() Updates the DSN string based on the config setting values.

# init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source (opens new window)

Throws

# updateDsn()

DEPRECATED

Deprecated in 3.4.0.

Updates the DSN string based on the config setting values.

View source (opens new window)

Throws

# Constants

Constant Description
DRIVER_MYSQL
DRIVER_PGSQL