Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 115 additions & 2 deletions docs/book/v7/installation/doctrine-orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Summary

This page covers setting up the database connection, creating and running migrations, and executing fixtures to populate the admin tables.
This page covers setting up the database connection, persistent connections, the second-level cache, creating and running migrations, and executing fixtures to populate the admin tables.

## Details

Expand All @@ -23,6 +23,7 @@ $databases = [
* Only one active connection is allowed at a time.
* By default, the application uses the 'mariadb' connection.
* You can switch to another connection by activating it under doctrine->connection->orm_default-->params.
* Set 'persistent' to true to reuse database connections across requests (supported by pdo_mysql and pdo_pgsql).
*/
'mariadb' => [
'host' => 'localhost',
Expand All @@ -31,16 +32,18 @@ $databases = [
'password' => '',
'port' => 3306,
'driver' => 'pdo_mysql',
'persistent' => false,
'collation' => 'utf8mb4_general_ci',
'table_prefix' => '',
],
'postgresql' => [
'host' => 'localhost',
'host' => '127.0.0.1',
'dbname' => 'dotkernel',
'user' => '',
'password' => '',
'port' => 5432,
'driver' => 'pdo_pgsql',
'persistent' => false,
'collation' => 'utf8mb4_general_ci',
'table_prefix' => '',
],
Expand All @@ -52,6 +55,90 @@ $databases = [
> By default, the application uses the 'mariadb' connection.
> You can switch to another connection by activating it under `doctrine` -> `connection` -> `orm_default` -> `params`.

### Persistent connections

Each entry in `$databases` has a `persistent` key, with the default value `false`.
Set it to `true` on the active connection to keep the database connection open after a request ends.
The next request that the same PHP process handles then uses this connection again and does not open a new one.

| Driver | `persistent` supported |
| --- | --- |
| `pdo_mysql`, `mysqli` | Yes |
| `pdo_pgsql` | Yes |
| `pgsql` | No |
| `pdo_sqlite`, `sqlite3` | No, the driver ignores the key |

> The `doctrine/dbal` documentation first describes persistent connections for MySQL and PostgreSQL in versions 4.3 and 3.10.
> Composer installs `doctrine/dbal` as a dependency of other packages.
> Run `composer show doctrine/dbal` to see your version.
> Older versions ignore the key.

Before you set the key to `true`, examine these items:

- **Connection limit:** each PHP-FPM worker keeps its own connection. The database `max_connections` value must be higher than the total number of workers in all pools and servers that connect to it.
- **State from the last request:** a connection that is used again keeps the state that the last request left on it. Examples are an open transaction after a fatal error, session variables, temporary tables and locks.
- **Idle timeouts:** the database can close a connection that stays idle for too long (the MariaDB `wait_timeout` value). The next request on that worker then fails with "server has gone away".
- **CLI:** commands get no benefit, because each command runs in its own process.
- **PostgreSQL:** a connection pooler such as PgBouncer is usually a better choice than persistent connections.

To make sure that the application uses connections again, send some requests and compare the connection IDs:

```sql
-- MariaDB
SHOW PROCESSLIST;

-- PostgreSQL
SELECT pid, backend_start FROM pg_stat_activity WHERE datname = 'dotkernel';
```

> If you installed the application before this setting was added, add `'persistent' => false,` to each entry in `$databases` in your `local.php` manually.

### Second-level cache

Starting with `doctrine/orm` 3.8.0, the Doctrine second-level cache needs the separate package `doctrine/orm-plugin-second-level-cache`.
The ORM only suggests that package, so Composer does not install it.
If the cache is enabled and the package is missing, Doctrine cannot create the entity manager, and the first request fails with this error:

```text
Service with name "doctrine.entity_manager.orm_default" could not be created.
Reason: Using the second level cache requires installing an additional package.
Install with composer require doctrine/orm-plugin-second-level-cache
```

The Core `ConfigProvider` enables the cache by default.
`config/autoload/local.php` disables it with the `enabled` key set to `false`.
The application loads `local.php` after the Core configuration.
This file changes only the `enabled` key, so the other second-level cache settings still come from Core:

```php
'doctrine' => [
'connection' => [
// ...
],
'configuration' => [
'orm_default' => [
'second_level_cache' => [
'enabled' => false,
],
],
],
],
```

The default application does not use the second-level cache.
No entity has `#[ORM\Cache]`, and no query calls `setCacheable(true)`.
The repositories use the query cache (`useQueryCache(true)`), which is a different cache. The `enabled` key has no effect on it.

To enable the second-level cache, install the plugin and then set `enabled` to `true`:

```shell
composer require doctrine/orm-plugin-second-level-cache:^1.0
```

> The installation script copies `config/autoload/local.php` from `local.php.dist` only one time, during the installation.
> If you installed the application before this setting was added, add the `configuration` block to your `local.php` manually.
> If configuration caching is enabled, delete `data/cache/config-cache.php` after this change.

### Creating migrations

Create a database migration by executing the following command:
Expand Down Expand Up @@ -132,6 +219,32 @@ A: You can create a **MariaDB** or **PostgreSQL** database, and its collation sh

A: Fill them out in `config/autoload/local.php`, under `$databases['mariadb']` (or `$databases['postgresql']` if using PostgreSQL).

**Q: Should I enable persistent connections?**

A: Enable them only after you make sure that the database `max_connections` value is sufficient for all the PHP-FPM workers that connect to it.
Persistent connections remove the cost of a new connection on each request, but each worker then keeps a connection open all the time.
See [Persistent connections](#persistent-connections).

**Q: I set `persistent` to `true`, but each request still opens a new connection. Why?**

A: Make sure that you changed the entry that `doctrine.connection.orm_default.params` points to.
Make sure that the driver is `pdo_mysql`, `mysqli` or `pdo_pgsql`.
Make sure that `doctrine/dbal` is version 4.3 (or 3.10) or later.
If configuration caching is enabled, delete `data/cache/config-cache.php`.

**Q: The first request fails with "Using the second level cache requires installing an additional package". What is wrong?**

A: Composer installed `doctrine/orm` 3.8.0 or later, and your configuration still enables the second-level cache.
These ORM versions need `doctrine/orm-plugin-second-level-cache` for that cache.
Set `doctrine.configuration.orm_default.second_level_cache.enabled` to `false` in `config/autoload/local.php`.
If you need the cache, install the plugin instead.
See [Second-level cache](#second-level-cache).

**Q: Does the application become slower when the second-level cache is disabled?**

A: Not in the default application, because it does not keep entities in that cache.
The cache is important only if you add `#[ORM\Cache]` to entities or call `setCacheable(true)` on queries.

**Q: How do I create and run a migration?**

A: Run `php ./vendor/bin/doctrine-migrations diff` to generate the migration file, then `php ./vendor/bin/doctrine-migrations migrate` to apply it.
Expand Down
7 changes: 7 additions & 0 deletions docs/book/v7/introduction/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ Versions below are the ones actually resolved by Composer, not a direct version

A: `doctrine/orm`, together with `doctrine/dbal` for database schema introspection and management.

**Q: Why did a `composer update` break the entity manager?**

A: `composer.json` does not declare `doctrine/orm`, so an update can install a new minor release.
`doctrine/orm` 3.8.0 moved the second-level cache into the separate package `doctrine/orm-plugin-second-level-cache`.
An installation that still enables that cache fails on the first request.
See [Second-level cache](../installation/doctrine-orm.md#second-level-cache).

**Q: Which framework underpins Dotkernel Admin?**

A: `mezzio/mezzio`, a PSR-15 middleware microframework, along with several Mezzio and Laminas components.
Expand Down