Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 0 additions & 122 deletions .docs/README.md

This file was deleted.

117 changes: 109 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,125 @@
Website 🚀 <a href="https://contributte.org">contributte.org</a> | Contact 👨🏻‍💻 <a href="https://f3l1x.io">f3l1x.io</a> | Twitter 🐦 <a href="https://twitter.com/contributte">@contributte</a>
</p>

## Usage
Doctrine-like events for Nextras ORM entity lifecycle hooks.

## Versions

| State | Version | Branch | Nette | PHP |
|----------|---------|--------------|--------|---------|
| dev | `^0.10` | `master` | `3.4+` | `>=8.1` |
| stable | `^0.9` | `master` | `3.1+` | `>=8.1` |

## Installation

To install latest version of `contributte/nextras-orm-events` use [Composer](https://getcomposer.org).

```bash
composer require contributte/nextras-orm-events
```

## Documentation
Register extension in your configuration.

For details on how to use this package, check out our [documentation](.docs).
```neon
extensions:
orm.events: Contributte\Nextras\Orm\Events\DI\NextrasOrmEventsExtension
```

## Versions
## Usage

| State | Version | Branch | Nette | PHP |
|----------|---------|--------------|--------|---------|
| dev | `^0.10` | `master` | `3.4+` | `>=8.1` |
| stable | `^0.9` | `master` | `3.1+` | `>=8.1` |
### Listener

Create listener.

```php
namespace App\Model;

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

final class BeforePersistListener implements BaseBeforePersistListener
{

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

}
```

Register it.

```neon
services:
- App\Model\ExampleBeforePersistListener
```

### Entity

Add lifecycle annotations to your entity or to a trait used by your entity.

```php
/**
* @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)
* @Flush(App\Model\FlushListener)
*
* @Lifecycle(App\Model\LifecycleListener)
*/
class Foo extends Entity
{
}
```

## Real Example

```neon
services:
- App\Model\FooBeforeInsertListener
```

```php
/**
* @BeforeInsert(App\Model\FooBeforeInsertListener)
*/
class Foo extends Entity
{
}
```

```php
// 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;
}
```

## Development

Expand Down