I want to integrate Doctrine ORM into my (non-Symfony) project. I already done this in another project and used the famous cli-config.php
into the project root directory.
But now, in my new project, I use the Symfony Console component and the Dependency Injection component (to reference services and commands, by tagging them).
1. I absolutely don't want to have a cli-config.php
in the project root. How the Sf Doctrine Bundle do this?
2. Also (but it is less important), I would like to have the Doctrine commands into my project CLI.
What would be the best way to do this? Create references to Doctrine commands into my services.yml
? Or create local "decorator commands" that call Doctrine commands via PHP?
Finally, after some googling and experiments, I found a complete solution.
Just read the doctrine.php in
vendor/bin
. It is very easy to avoid the hardcodedconfig-cli.php
file.1. Create an entity manager
In my case, I use a factory and this method hydrates the
doctrine.em
service.(
$config
is specific to my app, change values to use your own logic.)2. Merge Doctrine CLI commands in your CLI commands
Somewere in your code, like in some
bootstrap.php
, you probably declare yourSymfony\Component\Console\Application
command line interface, that's how I do this (theforeach
simply adds commands defined in myservices.yml
file):Now, we simply ask Doctrine to inject its commands into our Application:
That's it! You can also only add a subset of the Doctrine commands by using arsfeld's answer on this GitHub issue.
3. Bonus: only import needed commands and rename them
You can create decorator commands that inherit Doctrine commands (this is useful to redefine the name of Doctrine commands, as Symfony Doctrine Bundle does, eg.
orm:validate-schema
->doctrine:schema:validate
).To do this, remove the line
ConsoleRunner::addCommands($application);
we added in step 2. For each command you want to redefine, you will need to create an register a new command in your app. This command will "extends
" the target Doctrine command and will override theconfigure()
method.Here is an example with
orm:validate-schema
:Some Doctrine commands have aliases that will pollute your command namespaces, like
orm:generate-entities
andorm:generate:entities
. To remove these aliases, inconfigure()
, add->setAliases(array())
.Congratulations, you just redone the Symfony Doctrine Bundle :p (jk)