DbConfig
- Type
- Class
- Namespace
- craft\config
- Inherits
- craft\config\DbConfig » craft\config\BaseConfig » craft\base\Model » yii\base\Model (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
- Implements
- ArrayAccess (opens new window), IteratorAggregate (opens new window), craft\base\ModelInterface, yii\base\Arrayable (opens new window), yii\base\Configurable (opens new window), yii\base\StaticInstanceInterface (opens new window)
- Uses traits
- craft\base\ClonefixTrait, yii\base\ArrayableTrait (opens new window), yii\base\StaticInstanceTrait (opens new window)
- Since
- 3.0.0
DB config class
View source (opens new window)
# Public Properties
Property | Description |
---|---|
activeValidators (opens new window) | yii\validators\Validator (opens new window) – The validators applicable to the current scenario (opens new window). |
attributes | array (opens new window) – An array of key-value pairs of PDO attributes to pass into the PDO constructor. |
behaviors (opens new window) | yii\base\Behavior (opens new window) – List of behaviors attached to this component. |
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. |
errors (opens new window) | array (opens new window) – Errors for all attributes or the specified attribute. |
firstErrors (opens new window) | array (opens new window) – The first errors. |
iterator (opens new window) | ArrayIterator (opens new window) – An iterator for traversing the items in the list. |
password | string (opens new window) – The database password to connect with. |
port | integer (opens new window), null (opens new window) – The database server port. |
scenario (opens new window) | string (opens new window) – The scenario that this model is in. |
schema | string (opens new window), null (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), null (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. |
validators (opens new window) | ArrayObject (opens new window), yii\validators\Validator (opens new window) – All the validators declared in the model. |
# 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:
->attributes([
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
->charset('utf8mb4')
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
->collation('utf8mb4_0900_ai_ci')
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.
->database('mydatabase')
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.
->driver('mysql')
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
.
- MySQL parameters: https://php.net/manual/en/ref.pdo-mysql.connection.php (opens new window)
- PostgreSQL parameters: https://php.net/manual/en/ref.pdo-pgsql.connection.php (opens new window)
->dsn('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.
->password('super-secret')
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.
->port(3306)
View source (opens new window)
# schema
- Type
- string (opens new window), null (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.
->schema('myschema,public')
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
.
->server('localhost')
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.
->setSchemaOnConnect(true)
View source (opens new window)
# tablePrefix
- Type
- string (opens new window), null (opens new window)
- Default value
null
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.
->tablePrefix('craft_')
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.
->unixSocket('/Applications/MAMP/tmp/mysql/mysql.sock')
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.
->url('jdbc:mysql://database.foo:3306/mydb')
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.)
For more on Craft batch queries, see https://craftcms.com/knowledge-base/query-batching-batch-each (opens new window).
->useUnbufferedConnections(true)
View source (opens new window)
# user
- Type
- string (opens new window)
- Default value
'root'
The database username to connect with.
->user('db')
View source (opens new window)
# Protected Properties
Property | Description |
---|---|
filename | string (opens new window), null (opens new window) – The config filename |
renamedSettings | array (opens new window) – Settings that have been renamed |
# filename
- Type
- string (opens new window), null (opens new window)
- Default value
\craft\services\Config::CATEGORY_DB
The config filename
View source (opens new window)
# Public Methods
Method | Description |
---|---|
__call() (opens new window) | Calls the named method which is not a class method. |
__clone() | |
__construct() | |
__get() | Returns the value of a component property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets the value of a component property. |
__unset() (opens new window) | Sets a component property to be null. |
activeAttributes() (opens new window) | Returns the attribute names that are subject to validation in the current scenario. |
addError() (opens new window) | Adds a new error to the specified attribute. |
addErrors() (opens new window) | Adds a list of errors. |
addModelErrors() | Adds errors from another model, with a given attribute name prefix. |
afterValidate() (opens new window) | This method is invoked after validation ends. |
attachBehavior() (opens new window) | Attaches a behavior to this component. |
attachBehaviors() (opens new window) | Attaches a list of behaviors to the component. |
attributeHints() (opens new window) | Returns the attribute hints. |
attributeLabels() (opens new window) | Returns the attribute labels. |
attributes() (opens new window) | Returns the list of attribute names. |
beforeValidate() (opens new window) | This method is invoked before validation starts. |
behaviors() | Returns a list of behaviors that this component should behave as. |
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. |
charset() | The charset to use when creating tables. |
className() (opens new window) | Returns the fully qualified name of this class. |
clearErrors() (opens new window) | Removes errors for all attributes or a single attribute. |
collation() | The collation to use when creating tables. |
create() | Factory method for creating new config objects. |
createValidators() (opens new window) | Creates validator objects based on the validation rules specified in rules() (opens new window). |
database() | The name of the database to select. |
datetimeAttributes() | Returns the names of any attributes that should hold DateTime (opens new window) values. |
detachBehavior() (opens new window) | Detaches a behavior from the component. |
detachBehaviors() (opens new window) | Detaches all behaviors from the component. |
driver() | The database driver to use. Either mysql for MySQL or pgsql for PostgreSQL. |
dsn() | The Data Source Name (“DSN”) that tells Craft how to connect to the database. |
ensureBehaviors() (opens new window) | Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component. |
extraFields() (opens new window) | Returns the list of fields that can be expanded further and returned by toArray() (opens new window). |
fields() (opens new window) | Returns the list of fields that should be returned by default by toArray() (opens new window) when no specific fields are specified. |
formName() (opens new window) | Returns the form name that this model class should use. |
generateAttributeLabel() (opens new window) | Generates a user friendly attribute label based on the give attribute name. |
getActiveValidators() (opens new window) | Returns the validators applicable to the current scenario (opens new window). |
getAttributeHint() (opens new window) | Returns the text hint for the specified attribute. |
getAttributeLabel() (opens new window) | Returns the text label for the specified attribute. |
getAttributes() (opens new window) | Returns attribute values. |
getBehavior() (opens new window) | Returns the named behavior object. |
getBehaviors() (opens new window) | Returns all behaviors attached to this component. |
getErrorSummary() (opens new window) | Returns the errors for all attributes as a one-dimensional array. |
getErrors() (opens new window) | Returns the errors for all attributes or a single attribute. |
getFirstError() (opens new window) | Returns the first error of the specified attribute. |
getFirstErrors() (opens new window) | Returns the first error of every attribute in the model. |
getIterator() (opens new window) | Returns an iterator for traversing the attributes in the model. |
getScenario() (opens new window) | Returns the scenario that this model is used in. |
getValidators() (opens new window) | Returns all the validators declared in rules() (opens new window). |
hasErrors() | Returns a value indicating whether there is any validation error. |
hasEventHandlers() (opens new window) | Returns a value indicating whether there is any handler attached to the named event. |
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 for this component. |
init() | Initializes the object. |
instance() (opens new window) | Returns static class instance, which can be used to obtain meta information. |
isAttributeActive() (opens new window) | Returns a value indicating whether the attribute is active in the current scenario. |
isAttributeRequired() (opens new window) | Returns a value indicating whether the attribute is required. |
isAttributeSafe() (opens new window) | Returns a value indicating whether the attribute is safe for massive assignments. |
load() (opens new window) | Populates the model with input data. |
loadMultiple() (opens new window) | Populates a set of models with the data from end user. |
off() (opens new window) | Detaches an existing event handler from this component. |
offsetExists() (opens new window) | Returns whether there is an element at the specified offset. |
offsetGet() (opens new window) | Returns the element at the specified offset. |
offsetSet() (opens new window) | Sets the element at the specified offset. |
offsetUnset() (opens new window) | Sets the element value at the specified offset to null. |
on() (opens new window) | Attaches an event handler to an event. |
onUnsafeAttribute() (opens new window) | This method is invoked when an unsafe attribute is being massively assigned. |
password() | The database password to connect with. |
pdoAttributes() | An array of key-value pairs of PDO attributes to pass into the PDO constructor. |
port() | The database server port. Defaults to 3306 for MySQL and 5432 for PostgreSQL. |
rules() | Returns the validation rules for attributes. |
safeAttributes() (opens new window) | Returns the attribute names that are safe to be massively assigned in the current scenario. |
scenarios() (opens new window) | Returns a list of scenarios and the corresponding active attributes. |
schema() | The schema that Postgres is configured to use by default (PostgreSQL only). |
server() | The database server name or IP address. Usually localhost or 127.0.0.1 . |
setAttributes() | Sets the attribute values in a massive way. |
setScenario() (opens new window) | Sets the scenario for the model. |
setSchemaOnConnect() | Whether the schema() should be explicitly used for database queries (PostgreSQL only). |
tablePrefix() | 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. |
toArray() (opens new window) | Converts the model into an array. |
trigger() (opens new window) | Triggers an event. |
unixSocket() | 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. |
url() | The database connection URL, if one was provided by your hosting environment. |
useUnbufferedConnections() | Whether batched queries should be executed on a separate, unbuffered database connection. |
user() | The database username to connect with. |
validate() (opens new window) | Performs the data validation. |
validateMultiple() (opens new window) | Validates multiple models. |
# charset()
- Since
- 4.2.0
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
->charset('utf8mb4')
View source (opens new window)
Arguments
$value
(string (opens new window))
Returns
self
# collation()
- Since
- 4.2.0
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
->collation('utf8mb4_0900_ai_ci')
View source (opens new window)
Arguments
Returns
self
# database()
- Since
- 4.2.0
The name of the database to select.
->database('mydatabase')
View source (opens new window)
Arguments
Returns
self
Throws
# driver()
- Since
- 4.2.0
The database driver to use. Either mysql
for MySQL or pgsql
for PostgreSQL.
->driver('mysql')
View source (opens new window)
Arguments
Returns
self
Throws
# dsn()
- Since
- 4.2.0
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
.
- MySQL parameters: https://php.net/manual/en/ref.pdo-mysql.connection.php (opens new window)
- PostgreSQL parameters: https://php.net/manual/en/ref.pdo-pgsql.connection.php (opens new window)
->dsn('mysql:host=127.0.0.1;port=3306;dbname=acme_corp')
View source (opens new window)
Arguments
Returns
self
# 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
# password()
- Since
- 4.2.0
The database password to connect with.
->password('super-secret')
View source (opens new window)
Arguments
$value
(string (opens new window))
Returns
self
# pdoAttributes()
- Since
- 4.2.0
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:
->pdoAttributes([
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)
Arguments
$value
(array (opens new window))
Returns
self
# port()
- Since
- 4.2.0
The database server port. Defaults to 3306 for MySQL and 5432 for PostgreSQL.
->port(3306)
View source (opens new window)
Arguments
Returns
self
Throws
# schema()
- Since
- 4.2.0
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.
->schema('myschema,public')
See also https://www.postgresql.org/docs/8.2/static/ddl-schemas.html View source (opens new window)
Arguments
Returns
self
# server()
- Since
- 4.2.0
The database server name or IP address. Usually localhost
or 127.0.0.1
.
->server('localhost')
View source (opens new window)
Arguments
Returns
self
Throws
# setSchemaOnConnect()
- Since
- 4.2.0
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.
->setSchemaOnConnect()
View source (opens new window)
Arguments
$value
(boolean (opens new window))
Returns
self
# tablePrefix()
- Since
- 4.2.0
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.
->tablePrefix('craft_')
View source (opens new window)
Arguments
Returns
self
Throws
# unixSocket()
- Since
- 4.2.0
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.
->unixSocket('/Applications/MAMP/tmp/mysql/mysql.sock')
View source (opens new window)
Arguments
Returns
self
Throws
# url()
- Since
- 4.2.0
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.
->url('jdbc:mysql://database.foo:3306/mydb')
View source (opens new window)
Arguments
Returns
self
# useUnbufferedConnections()
- Since
- 4.2.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.)
For more on Craft batch queries, see https://craftcms.com/knowledge-base/query-batching-batch-each (opens new window).
->useUnbufferedConnections()
View source (opens new window)
Arguments
$value
(boolean (opens new window))
Returns
self
# user()
- Since
- 4.2.0
The database username to connect with.
->user('db')
View source (opens new window)
Arguments
$value
(string (opens new window))
Returns
self
# Protected Methods
Method | Description |
---|---|
defineBehaviors() | Returns the behaviors to attach to this class. |
defineRules() | Returns the validation rules for attributes. |
extractFieldsFor() (opens new window) | Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id". |
extractRootFields() (opens new window) | Extracts the root field names from nested fields. |
resolveFields() (opens new window) | Determines which fields can be returned by toArray() (opens new window). |
# Constants
Constant | Description |
---|---|
DRIVER_MYSQL | |
DRIVER_PGSQL | |
SCENARIO_DEFAULT | The name of the default scenario. |