22
33## Composer Requirements
44
5- The first step is to add alongside your current packages the required entries for our Doctrine installation. We would add the following to our ` composer.json ` file located in our root folder:
5+ The first step is to add alongside your current packages the required entries for our Doctrine installation.
6+ We would add the following to our ` composer.json ` file located in our root folder:
67
7- ![ composer] ( ../images/composer.png )
8+ ![ composer] ( images/composer.png )
9+
10+ ``` text
11+ "dotkernel/dot-cache": "^4.0",
12+ "ramsey/uuid": "^4.5.0",
13+ "ramsey/uuid-doctrine": "^2.1.0",
14+ "roave/psr-container-doctrine": "^5.2.2 || ^6.0.0",
15+ ```
816
917` dotkernel/dot-cache `
1018
@@ -39,7 +47,35 @@ After successfully installing our dependencies we now need to configure our Doct
3947
4048In the file ` config/autoload/local.php ` the structure would be updated like this:
4149
42- ![ local.php] ( ../images/local.png )
50+ ![ local.php] ( images/local.png )
51+
52+ ``` php
53+ $databases = [
54+ 'default' => [
55+ 'host' => 'localhost',
56+ 'dbname' => 'light',
57+ 'user' => 'root',
58+ 'password' => '123',
59+ 'port' => 3306,
60+ 'driver' => 'pdo_mysql',
61+ 'charset' => 'utf8mb4',
62+ 'collate' => 'utf8mb4_general_ci',
63+ ],
64+ // you can add more database connections into this array
65+ ];
66+
67+ return [
68+ 'databases' => $databases,
69+ 'doctrine' => [
70+ 'connection' => [
71+ 'orm_default' => [
72+ 'params' => $databases['default'],
73+ ],
74+ ],
75+ ],
76+ // the rest of your configuration
77+ ];
78+ ```
4379
4480### Declare the Doctrine Drivers and Migrations Location
4581
@@ -48,29 +84,82 @@ This package takes all the provided configs from the `config/config.php` file an
4884
4985Our new ` src/App/src/ConfigProvider.php ` class would look like this now:
5086
51- ![ Config provider factories update] ( ../ images/config-provider-1.png)
52- ![ Doctrine config function] ( ../ images/config-provider-2.png)
87+ ![ Config provider factories update] ( images/config-provider-1.png )
88+ ![ Doctrine config function] ( images/config-provider-2.png )
5389
5490We also require a new file ` config/cli-config.php ` .
5591It initializes and returns a ` DependencyFactory ` that Doctrine Migrations uses to run migrations.
5692
57- ![ cli-config] ( ../images/cli-config.png )
93+ ![ cli-config] ( images/cli-config.png )
94+
95+ ``` php
96+ <?php
97+
98+ declare(strict_types=1);
99+
100+ use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
101+ use Doctrine\Migrations\Configuration\Migration\ConfigurationArray;
102+ use Doctrine\Migrations\DependencyFactory;
103+ use Doctrine\ORM\EntityManager;
104+
105+ $container = require 'config/container.php';
106+
107+ $entityManager = $container->get(EntityManager::class);
108+ $entityManager->getEventManager();
109+
110+ return DependencyFactory::fromEntityManager(
111+ new ConfigurationArray($container->get('config')['doctrine']['migrations']),
112+ new ExistingEntityManager($entityManager)
113+ );
114+ ```
58115
59116## Running doctrine
60117
61118Now that everything has been configured we only need to do one last thing, to create an executable for the Doctrine CLI.
62- In our case we will create it as ` /bin/doctrine `
119+ In our case we will create a ` doctrine ` file inside the application's ` bin ` directory:
120+
121+ ![ bin/doctrine] ( images/doctrine.png )
122+
123+ ``` php
124+ #!/usr/bin/env php
125+ <?php
63126
64- ![ bin/doctrine ] ( ../images/doctrine.png )
127+ declare(strict_types=1);
65128
66- (Optional) To keep things tidy we recommend to make an executable for the migrations of Doctrine as well for example ` /bin/doctrine-migrations ` :
129+ use Doctrine\ORM\EntityManager;
130+ use Doctrine\ORM\Tools\Console\ConsoleRunner;
131+ use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
67132
68- ![ bin/doctrine-migrations] ( ../images/doctrine-migrations.png )
133+ require_once 'vendor/autoload.php';
134+
135+ $container = require 'config/container.php';
136+
137+ $entityManager = $container->get(EntityManager::class);
138+ $entityManager->getEventManager();
139+
140+ ConsoleRunner::run(new SingleManagerProvider($entityManager));
141+ ```
142+
143+ (Optional) To keep things tidy, we recommend making an executable for the migrations of Doctrine as well.
144+ For this, we create ` doctrine-migrations ` file inside the application's ` bin ` directory:
145+
146+ ![ bin/doctrine-migrations] ( images/doctrine-migrations.png )
147+
148+ ``` php
149+ #!/usr/bin/env php
150+ <?php
151+
152+ declare(strict_types=1);
153+
154+ namespace Doctrine\Migrations;
155+
156+ require __DIR__ . '/../vendor/doctrine/migrations/bin/doctrine-migrations.php';
157+ ```
69158
70159Now by running the command bellow we should see the Doctrine CLI version alongside its available commands:
71160
72161``` shell
73- php bin/doctrine
162+ php ./ bin/doctrine
74163```
75164
76165Example (truncated) output:
0 commit comments