Latest release: v0.2.2

Nettrine / ORM

Content

Minimal configuration

At first, you will need the Doctrine DBAL extension. Take a look at Nettrine DBAL in this organization. Install nettrine/dbal package using composer.

composer require nettrine/dbal
1

Place DbalExtension in your neon config file.

extensions:
    dbal: Nettrine\DBAL\DI\DbalExtension
1
2

And set-up DBAL connection.

dbal:
    connection:
        host: 127.0.0.1
        user: root
        password:
        dbname: nettrine
        #driver: pdo_pgsql
1
2
3
4
5
6
7

Secondly, enable the Doctrine ORM extension. It's provided by this package.

extensions:
    orm: Nettrine\ORM\DI\OrmExtension
1
2

Pick any metadata provider, for example annotations (they are widely used). We have a special extension for annotations (Nettrine\ORM\DI\OrmAnnotationsExtension).

extensions:
    orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension

orm.annotations:
    paths:
        - App/Model/Database/Entity
1
2
3
4
5
6

You can find more examples in our playground repository.

OrmExtension

OrmExtension has a few options you can configure. Let's take a look at them.

EntityManagerDecorator

Defining your own EntityManagerDecorator is useful for adding or overriding methods you need.

orm:
    entityManagerDecoratorClass: App\Model\Database\EntityManagerDecorator
1
2
namespace App\Model\Database;

use Nettrine\ORM\EntityManagerDecorator as NettrineEntityManagerDecorator;

class EntityManagerDecorator extends NettrineEntityManagerDecorator
{

}

1
2
3
4
5
6
7
8
9

Configuration

List of all configuration options:

orm:
    configuration:
        proxyDir: '%tempDir%/proxies'
        autoGenerateProxyClasses: NULL
        proxyNamespace: 'Nettrine\Proxy'
        metadataDriverImpl: NULL
        entityNamespaces: []
        customStringFunctions: []
        customNumericFunctions: []
        customDatetimeFunctions: []
        customHydrationModes: []
        classMetadataFactoryName: NULL
        defaultRepositoryClassName: NULL
        namingStrategy: Doctrine\ORM\Mapping\UnderscoreNamingStrategy
        quoteStrategy: NULL
        entityListenerResolver: NULL
        repositoryFactory: NULL
        defaultQueryHints: []
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

At this time we support only 1 connection, the default connection. If you need more connections (more databases?), please open an issue or send a PR. Thanks.

Bridges

The compiler extensions would be so big that we decided to split them into more separate files / compiler extensions.

Annotations Bridge

Are you using annotations in your entities?

/**
 * @ORM\Entity
 */
class Category
{
1
2
3
4
5

You will need the OrmAnnotationsExtension. This is the default configuration, it uses the filesystem cache driver.

extensions:
    orm: Nettrine\ORM\DI\OrmExtension
    orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension

orm.annotations:
    paths: []
    excludePaths: []
    ignore: []
    defaultCache: filesystem
1
2
3
4
5
6
7
8
9

Available drivers:

  • apc - ApcCache
  • apcu - ApcuCache
  • array - ArrayCache
  • filesystem - FilesystemCache
  • memcache - MemcacheCache
  • memcached - MemcachedCache
  • redis - RedisCache
  • void - VoidCache
  • xcache - XcacheCache

You can change cache settings for the annotation reader.

orm.annotations: 
    defaultCache: apcu

    # or directly
    cache: @cacheReader
1
2
3
4
5

Cache Bridge

This extension sets up cache for all important parts: queryCache, resultCache, hydrationCache, metadataCache and secondLevelCache.

This is the default configuration, it uses the filesystem driver.

extensions:
    orm: Nettrine\ORM\DI\OrmExtension
    orm.cache: Nettrine\ORM\DI\OrmCacheExtension

orm.cache:
    defaultDriver: filesystem
1
2
3
4
5
6

Available drivers:

  • apc - ApcCache
  • apcu - ApcuCache
  • array - ArrayCache
  • filesystem - FilesystemCache
  • memcache - MemcacheCache
  • memcached - MemcachedCache
  • redis - RedisCache
  • void - VoidCache
  • xcache - XcacheCache

You can also specify a single driver. Or change the orm.cache.defaultDriver for all of them.

orm.cache:
    queryCache: App\SpecialDriver
    resultCache: App\SpecialOtherDriver
    hydrationCache: App\SpecialDriver('foo')
    metadataCache: @cacheDriver
    secondLevelCache: @cacheDriverFactory::create('bar')
1
2
3
4
5
6

Console Bridge

This package works well with Symfony/Console. Take a look at contributte/console tiny integration for Nette Framework.

extensions:
    # Console
    console: Contributte\Console\DI\ConsoleExtension

    # Orm
    orm: Nettrine\ORM\DI\OrmExtension
    orm.console: Nettrine\ORM\DI\OrmConsoleExtension
1
2
3
4
5
6
7

Since this moment you can use all registered Doctrine ORM commands using bin/console.

Commands

Other features

Id attribute

You can use the predefined Id trait in your Entities.


use Nettrine\ORM\Entity\Attributes\Id;

/**
 * @ORM\Entity
 */
class Category
{

    use Id;

}
1
2
3
4
5
6
7
8
9
10
11
12

Entity Mapping

You can use the predefined TEntityMapping trait in your extensions.


use Nette\DI\CompilerExtension;
use Nettrine\ORM\DI\TEntityMapping;

class CategoryExtension extends CompilerExtension
{

    use TEntityMapping;

    public function loadConfiguration(): void
    {
        $this->setEntityMappings([
            'Forum' => __DIR__ . '/../Entity',
        ]);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16