Statistics
2537
3
1
129d
Badges
Requirements

Nextras\ORM Events

Doctrine-like events for Nextras\ORM Entity.


Build Status Code coverage Downloads this Month Downloads total Latest stable HHVM Status

Discussion / Help

Join the chat

Install

composer require minetro/nextras-orm-events

Usage

Config

extensions:
    ormEvents: Minetro\Nextras\Events\DI\NextrasEventsExtension
services:
    - My\BeforePersistListener

Entity

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

/**
 * @BeforeInsert(My/BeforeInsertListener)
 * @BeforePersist(My/BeforePersistListener)
 * @BeforeRemove(My/BeforeRemoveListener)
 * @BeforeUpdate(My/BeforeUpdateListener)
 * @AfterInsert(My/AfterInsertListener)
 * @AfterPersist(My/AfterPersistListener)
 * @AfterRemove(My/AfterRemoveListener)
 * @AfterUpdate(My/AfterUpdateListener)
 
 * @Lifecycle(My/LifecycleListener)
 */
class Foo extends Entity
{
}

Service


namespace My;

use Minetro\Nextras\Events\Listeners\BeforePersistListener;

final class BeforePersistListener implements BeforePersistListener
{

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

}

Real example

service:
    - FooBeforeInsertListener
/**
 * @BeforeInsert(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('FooBeforeInsertListener'),
        'onBeforeInsert',
    ];
    
    // ===== attaching events ============================
    
    return $service;
}

That's all. Super ultra simple.


Thanks for testing, reporting and contributing.

  • 0.2 v0.2

    Bumped! 🌾

  • 0.1 v0.1

    First bump! 🙆‍♂️

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

Componette Componette admin@componette.com