How to use Doctrine ORM with Nette Framework

8 days ago by Rick Strafy  

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.

Console extension

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.

Doctrine extensions

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 doctrine.neon (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 the file.

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:
    paths:
        - app/Model # The root folder where to start search for entities

doctrine.dbal:
    debug:
        panel: %debugMode%
    connection:
        driver: pdo_mysql

After that, last thing we need is to add database credentials into our local.neon file.

doctrine.dbal:
    connection:
        host: localhost
        user: root
        password: password
        dbname: demo

You can now create entities and update database schema using command

bin/console orm:schema-tool:update --dump-sql --force

This is just an quick example, if you need more advanced configuration, caching options, migrations, custom functions or column types, visit Nettrine Documentation.

Comments (RSS)

  1. 😍

    7 days ago

Sign in to submit a comment