How to use Doctrine ORM with Nette Framework
In this article, I'll try to provide you step by step guide, how to use Doctrine ORM with Nette FW. Most recommended Doctrine integration to Nette is called Nettrine, it's maintained by the contributte team.
So, first things first, for proper Doctrine usage we'll need symfony/console,
for console commands, you can install nette integration via composer,
composer require contributte/console and then register extension in
the main neon config file.
extensions: console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
You also need create bin directory and console file in that directory with this content and add executable permission to the console file.
We'll need to install only one more extension, because dbal, cache and
annotations are installed automatically as dependencies
composer require nettrine/orm.
After successful installation we need to register those extensions in the
neon configuration, best way is to create new file
(where you should also have
local.neon file which is included into
Bootstrap class) in configuration folder and include it to the main neon file
(probably config.neon or common.neon) by adding this to the end of
includes: - doctrine.neon
Now we need to register and configure doctrine extensions, here is some
example of configured
doctrine.neon file with pdo_mysql driver.
extensions: doctrine.annotations: Nettrine\Annotations\DI\AnnotationsExtension doctrine.dbal: Nettrine\DBAL\DI\DbalExtension doctrine.dbal.console: Nettrine\DBAL\DI\DbalConsoleExtension doctrine.orm: Nettrine\ORM\DI\OrmExtension doctrine.orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension doctrine.orm.console: Nettrine\ORM\DI\OrmConsoleExtension doctrine.cache: Nettrine\Cache\DI\CacheExtension doctrine.orm.annotations: mapping: App\Model: %appDir%/Model # The root folder where to start search for entities doctrine.dbal: debug: panel: %debugMode% connection: driver: pdo_mysql host: %doctrine.host% user: %doctrine.user% password: %doctrine.password% dbname: %doctrine.dbname%
After that, last thing we need is to add database credentials into our
parameters: doctrine: host: localhost user: user password: password dbname: test
You can now create entities and update database schema using command
bin/console orm:schema-tool:update --dump-sql --force