# Content

Doctrine (KnpLabs/DoctrineBehaviors (opens new window)) extension for Nette Framework

# Setup

Install package

composer require nettrine/extensions-knplabs
1

Register extension

extensions:
    nettrine.extensions.knplabs: Nettrine\Extensions\KnpLabs\DI\KnpLabsBehaviorExtension
1
2

# Configuration

By default all behaviors are disabled, you have to enable them.

Most of the behaviors include a subscriber. If you use nettrine/dbal (opens new window) then they are configured automatically. Otherwise you have to add them to the Event manager.

Behaviors blameable, geocodable, sluggable, softDeletable, sortable, timestampable, translatable and tree have option trait (or *Trait) which allows you to swap the implementation.

Behaviors blameable, geocodable, loggable and translatable accept a callable. You may use all of following syntaxes:

# Static method call (or any other valid callable, like 'someFunction')
exampleCallable: [StaticClass, 'calledMethod']


exampleCallable: [@service, 'calledMethod'],

# Reference to service which implements __invoke()
exampleCallable: @serviceWhichImplements__invoke()

# Register and use new service which implements __invoke(), like you would in 'services' config section
exampleCallable:
    factory: Your\Special\Service
1
2
3
4
5
6
7
8
9
10
11
12

# Blameable (opens new window)

Enable behavior

nettrine.extensions.knplabs:
    blameable: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    blameable:
        userCallable: callable ## returns object specified by userEntity or object which implements __toString() or string
        userEntity: Your\User\Entity
        trait: YourBlameableTrait
1
2
3
4
5

You may use Nettrine\Extensions\KnpLabs\Security\UserCallable for Nette\Security\User::getId()

Use trait in entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class BlameableEntity
{

	use Behaviors\Blameable\Blameable;

}
1
2
3
4
5
6
7
8
9

# Filterable (opens new window)

Use trait in repository and implement abstract methods

use Doctrine\ORM\EntityRepository;
use Knp\DoctrineBehaviors\ORM as Behaviors;

final class FilterableRepository extends EntityRepository
{

    use Behaviors\Filterable\FilterableRepository;

    public function getLikeFilterColumns(): array
    {
        // TODO: Implement getLikeFilterColumns() method.
    }

    public function getILikeFilterColumns(): array
    {
        // TODO: Implement getILikeFilterColumns() method.
    }

    public function getEqualFilterColumns(): array
    {
        // TODO: Implement getEqualFilterColumns() method.
    }

    public function getInFilterColumns(): array
    {
        // TODO: Implement getInFilterColumns() method.
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# Geocodable (opens new window)

Enable behavior

nettrine.extensions.knplabs:
    geocodable: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    geocodable:
        geolocationCallable: callable # accepts entity, returns Point|false
        trait: YourGeocodableTrait
1
2
3
4

Use trait in entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class GeocodableEntity
{

	use Behaviors\Geocodable\Geocodable;

}
1
2
3
4
5
6
7
8
9

Use trait in repository

use Doctrine\ORM\EntityRepository;
use Knp\DoctrineBehaviors\ORM as Behaviors;

final class GeocodableRepository extends EntityRepository
{

    use Behaviors\Geocodable\GeocodableRepository;

}
1
2
3
4
5
6
7
8
9

# Joinable

Use trait in repository

use Doctrine\ORM\EntityRepository;
use Knp\DoctrineBehaviors\ORM as Behaviors;

final class JoinableRepository extends EntityRepository
{

    use Behaviors\Joinable\JoinableRepository;

}
1
2
3
4
5
6
7
8
9

# Loggable (opens new window)

Enable behavior

nettrine.extensions.knplabs:
    loggable:
        loggerCallable: callable # accepts message
1
2
3

You may use Knp\DoctrineBehaviors\ORM\Loggable\LoggerCallable for PSR-3 logger or Nettrine\Extensions\KnpLabs\Tracy\LoggerCallable for Tracy logger.

Use trait in entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class LoggableEntity
{

	use Behaviors\Loggable\Loggable;

}
1
2
3
4
5
6
7
8
9

# Sluggable (opens new window)

Enable behavior

nettrine.extensions.knplabs:
    sluggable: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    sluggable:
        trait: YourSluggableTrait
1
2
3

Use trait in entity and implement abstract methods

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class SluggableEntity
{

	use Behaviors\Sluggable\Sluggable;

    public function getSluggableFields(): array
    {
        // TODO: Implement getSluggableFields() method.
    }

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

# SoftDeletable (opens new window)

Enable behavior

nettrine.extensions.knplabs:
    softDeletable: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    softDeletable:
        trait: YourSoftDeletableTrait
1
2
3

Use trait in entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class SoftDeletableEntity
{

	use Behaviors\SoftDeletable\SoftDeletable;

}
1
2
3
4
5
6
7
8
9

# Sortable

Enable behavior

nettrine.extensions.knplabs:
    sortable: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    sortable:
        trait: YourSortableTrait
1
2
3

Use trait in entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class SortableEntity
{

	use Behaviors\Sortable\Sortable;

}
1
2
3
4
5
6
7
8
9

Use trait in repository

use Doctrine\ORM\EntityRepository;
use Knp\DoctrineBehaviors\ORM as Behaviors;

final class SortableRepository extends EntityRepository
{

    use Behaviors\Sortable\SortableRepository;

}
1
2
3
4
5
6
7
8
9

# Timestampable (opens new window)

Enable behavior

nettrine.extensions.knplabs:
    timestampable: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    timestampable:
        dbFieldType: datetime | datetimetz | timestamp | timestamptz | ... # default: datetime
        trait: YourTimestampableTrait
1
2
3
4

Use trait in entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class TimestampableEntity
{

	use Behaviors\Timestampable\Timestampable;

}
1
2
3
4
5
6
7
8
9

# Translatable

Enable behavior

nettrine.extensions.knplabs:
    translatable: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    translatable:
        currentLocaleCallable: callable # returns current locale or null
        defaultLocaleCallable: callable # returns default locale or null
        translatableFetchMode: LAZY | EAGER | EXTRA_LAZY # default: LAZY
        translationFetchMode: LAZY | EAGER | EXTRA_LAZY # default: LAZY
        translatableTrait: YourTranslatableTrait
        translationTrait: YourTranslationTrait
1
2
3
4
5
6
7
8

Use trait in entity

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class TranslatableEntity
{

	use Behaviors\Translatable\Translatable;

}
1
2
3
4
5
6
7
8
9

# Tree (opens new window)

Enable behavior

nettrine.extensions.knplabs:
    tree: true
1
2

Or, if you want to add additional options

nettrine.extensions.knplabs:
    tree:
        nodeTrait: YourTreeNodeTrait
1
2
3

Use trait in entity and implement interface

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as Behaviors;

class TreeEntity implements Behaviors\Tree\NodeInterface
{

	use Behaviors\Tree\Node;

}
1
2
3
4
5
6
7
8
9

Use trait in repository

use Doctrine\ORM\EntityRepository;
use Knp\DoctrineBehaviors\ORM as Behaviors;

final class TreeRepository extends EntityRepository
{

    use Behaviors\Tree\Tree;

}
1
2
3
4
5
6
7
8
9