Nette Documentation Preview

syntax
Configuring Database
********************

.[perex]
Overview of configuration options for the Nette Database.

If you are not using the whole framework, but only this library, read [how to load the configuration|bootstrap:].


Single Connection
-----------------

Configure a single database connection:

```neon
database:
	# DSN, only mandatory key
	dsn: "sqlite:%appDir%/Model/demo.db"
	user: ...
	password: ...
```

Creates the services `Nette\Database\Connection` and `Nette\Database\Explorer`, which are usually passed by [autowiring |dependency-injection:autowiring] or by referring to [their name |#DI Services].

Other settings:

```neon
database:
	# shows database panel in Tracy Bar?
	debugger: ...     # (bool) defaults to true

	# shows query EXPLAIN in Tracy Bar?
	explain: ...      # (bool) defaults to true

	# to enable autowiring for this connection?
	autowired: ...    # (bool) defaults to true for first connection

	# table conventions: discovered, static, or class name
	conventions: discovered  # (string) defaults to 'discovered'

	options:
		# to connect to the database only when needed?
		lazy: ...     # (bool) defaults to false

		# PHP database driver class
		driverClass:  # (string)

		# MySQL only: sets sql_mode
		sqlmode:      # (string)

		# MySQL only: sets SET NAMES
		charset:      # (string) defaults to 'utf8mb4'

		# MySQL only: converts TINYINT(1) to bool
		convertBoolean: # (bool) defaults to false

		# returns date columns as immutable objects (since version 3.2.1)
		newDateTime:  # (bool) defaults to false

		# only Oracle and SQLite: date format
		formatDateTime: # (string) defaults to 'U'
```

The `options` key can contain other options that can be found in the [PDO driver documentation |https://www.php.net/manual/en/pdo.drivers.php], such as:

```neon
database:
	options:
		PDO::MYSQL_ATTR_COMPRESS: true
```


Multiple Connections
--------------------

In the configuration we can define more database connections by dividing them into named sections:

```neon
database:
	main:
		dsn: 'mysql:host=127.0.0.1;dbname=test'
		user: root
		password: password

	another:
		dsn: 'sqlite::memory:'
```

Autowiring is enabled only for services from the first section. This can be changed using `autowired: false` or `autowired: true`.


DI Services
-----------

These services are added to the DI container, where `###` represents the connection name:

| Name | Type | Description
|----------------------------------------------------------
| `database.###.connection` | [api:Nette\Database\Connection] | database connection
| `database.###.explorer` | [api:Nette\Database\Explorer] | [Database Explorer |explorer]


If we define only one connection, the service names will be `database.default.connection` and `database.default.explorer`. If we define multiple connections as in the example above, the names will correspond to the sections, i.e. `database.main.connection`, `database.main.explorer` and then `database.another.connection` and `database.another.explorer`.

We pass the non-authenticated services explicitly by referring to their name:

```neon
services:
	- UserFacade(@database.another.connection)
```

Configuring Database

Overview of configuration options for the Nette Database.

If you are not using the whole framework, but only this library, read how to load the configuration.

Single Connection

Configure a single database connection:

database:
	# DSN, only mandatory key
	dsn: "sqlite:%appDir%/Model/demo.db"
	user: ...
	password: ...

Creates the services Nette\Database\Connection and Nette\Database\Explorer, which are usually passed by autowiring or by referring to their name.

Other settings:

database:
	# shows database panel in Tracy Bar?
	debugger: ...     # (bool) defaults to true

	# shows query EXPLAIN in Tracy Bar?
	explain: ...      # (bool) defaults to true

	# to enable autowiring for this connection?
	autowired: ...    # (bool) defaults to true for first connection

	# table conventions: discovered, static, or class name
	conventions: discovered  # (string) defaults to 'discovered'

	options:
		# to connect to the database only when needed?
		lazy: ...     # (bool) defaults to false

		# PHP database driver class
		driverClass:  # (string)

		# MySQL only: sets sql_mode
		sqlmode:      # (string)

		# MySQL only: sets SET NAMES
		charset:      # (string) defaults to 'utf8mb4'

		# MySQL only: converts TINYINT(1) to bool
		convertBoolean: # (bool) defaults to false

		# returns date columns as immutable objects (since version 3.2.1)
		newDateTime:  # (bool) defaults to false

		# only Oracle and SQLite: date format
		formatDateTime: # (string) defaults to 'U'

The options key can contain other options that can be found in the PDO driver documentation, such as:

database:
	options:
		PDO::MYSQL_ATTR_COMPRESS: true

Multiple Connections

In the configuration we can define more database connections by dividing them into named sections:

database:
	main:
		dsn: 'mysql:host=127.0.0.1;dbname=test'
		user: root
		password: password

	another:
		dsn: 'sqlite::memory:'

Autowiring is enabled only for services from the first section. This can be changed using autowired: false or autowired: true.

DI Services

These services are added to the DI container, where ### represents the connection name:

Name Type Description
database.###.connection Nette\Database\Connection database connection
database.###.explorer Nette\Database\Explorer Database Explorer

If we define only one connection, the service names will be database.default.connection and database.default.explorer. If we define multiple connections as in the example above, the names will correspond to the sections, i.e. database.main.connection, database.main.explorer and then database.another.connection and database.another.explorer.

We pass the non-authenticated services explicitly by referring to their name:

services:
	- UserFacade(@database.another.connection)