Latest release: 0.4

Nextras ORM Events

Content

Usage

Config

extensions:
    orm.events: Contributte\Nextras\Orm\Events\DI\NextrasOrmEventsExtension
1
2
services:
    - App\Model\BeforePersistListener
1
2

Entity

Just add annotation @<Before/Update> to your entity.

/**
 * @BeforeInsert(App\Model\BeforeInsertListener)
 * @BeforePersist(App\Model\BeforePersistListener)
 * @BeforeRemove(App\Model\BeforeRemoveListener)
 * @BeforeUpdate(App\Model\BeforeUpdateListener)
 * @AfterInsert(App\Model\AfterInsertListener)
 * @AfterPersist(App\Model\AfterPersistListener)
 * @AfterRemove(App\Model\AfterRemoveListener)
 * @AfterUpdate(App\Model\AfterUpdateListener)

 * @Lifecycle(App\Model\LifecycleListener)
 */
class Foo extends Entity
{
}

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

Service


namespace App\Model;

use Contributte\Nextras\Orm\Events\Listeners\BeforePersistListener;
use Nextras\Orm\Entity\IEntity;

final class BeforePersistListener implements BeforePersistListener
{

    public function onBeforePersist(IEntity $entity): void
    {
        // ...
    }

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

Real example

service:
    - App\Model\FooBeforeInsertListener
1
2
/**
 * @BeforeInsert(App\Model\FooBeforeInsertListener)
 */
class Foo extends Entity
{
}
1
2
3
4
5
6
// Generated container..

/**
 * @return FooRepository
 */
public function createServiceOrm__repositories__foo()
{
    $service = new FooRepository(
        $this->getService('orm.mappers.foo'),
        $this->getService('orm.dependencyProvider')
    );
    $service->setModel($this->getService('orm.model'));

    // ===== attaching events (provided by extension =====

    $service->onBeforeInsert[] = [
        $this->getService('App\Model\FooBeforeInsertListener'),
        'onBeforeInsert',
    ];

    // ===== attaching events ============================

    return $service;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24