How to use Doctrine ORM with Nette Framework

4 years 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:
    # Use it only if you are using annotations instead of php8 attributes
    # 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.attributes: Nettrine\ORM\DI\OrmAttributesExtension
    nettrine.orm.cache: Nettrine\ORM\DI\OrmCacheExtension
	# Use it only if you are using annotations instead of php8 attributes
    # doctrine.orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension
    doctrine.orm.console: Nettrine\ORM\DI\OrmConsoleExtension
    doctrine.cache: Nettrine\Cache\DI\CacheExtension

doctrine.orm.attributes: # If you still use annotations, change to 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 local.neon file.

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

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. 😍

    4 years ago
  2. nieje tam chyba v
    doctrine.orm.annotations:
    paths:
     – app/Model # The root folder where to start search for entities

    to hádže errory a vyžaduje mať nastavený mapping

    4 years ago · replied [3] Rick Strafy
  3. #2 account23 Hi, I didn't noticed the comment, it's fixed, there was change in config structure of the nettrine dbal, it's now mapping with namespaces, not paths

    4 years ago

Sign in to submit a comment