Statistics
9982
5
1
33d
Badges
Tags
orm
Dependencies

Nextras ORM Events

Doctrine-like events for Nextras ORM entity lifecycle.


Build Status Code coverage Licence

Downloads this Month Downloads total Latest stable

Discussion / Help

Join the chat

Install

composer require contributte/nextras-orm-events

Versions

State Version Branch PHP
development ^0.4 master >= 5.6
stable ^0.3 master >= 5.6
stable ^0.2 master >= 5.6 (old namespace)

Usage

Config

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

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
{
}

Service


namespace App\Model;

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

final class BeforePersistListener implements BeforePersistListener
{

    /**
     * @param IEntity $entity
     * @return void
     */
    public function onBeforePersist(IEntity $entity)
    {
        // ...
    }

}

Real example

service:
    - App\Model\FooBeforeInsertListener
/**
 * @BeforeInsert(App\Model\FooBeforeInsertListener)
 */
class Foo extends Entity
{
}
// 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;
}

That's all. Super ultra simple.

Maintainers


Milan Felix Šulc

Thank you for testing, reporting and contributing.

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com