Skip to content

Complete example for Messenger with persistence #1293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2021
Merged
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
51 changes: 50 additions & 1 deletion core/data-persisters.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,53 @@ final class BlogPostDataPersister implements ContextAwareDataPersisterInterface,
}
```

This is very useful when using [`Messenger` with API Platform](messenger.md) as you may want to do something asynchronously with the data but still call the default Doctrine data persister.
This is very useful when using [`Messenger` with API Platform](messenger.md) as you may want to do something asynchronously with the data but still call the default Doctrine data persister, for example:
```php
namespace App\DataPersister;

use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\BlogPost;

final class BlogPostDataPersister implements ContextAwareDataPersisterInterface, ResumableDataPersisterInterface
{
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

public function supports($data, array $context = []): bool
{
return $data instanceof BlogPost;
}

public function persist($data, array $context = [])
{
$this->entityManager->persist($data);
$this->entityManager->flush();
}

public function remove($data, array $context = [])
{
$this->entityManager->remove($data);
$this->entityManager->flush();
}

// Once called this data persister will resume to the next one
public function resumable(array $context = []): bool
{
return true;
}
}
```
```yaml
# api/config/services.yaml
services:
# ...
App\DataPersister\BlogPostDataPersister: ~
# Uncomment only if autoconfiguration is disabled
#arguments: ['@App\DataPersister\BlogPostDataPersister.inner']
#tags: [ 'api_platform.data_persister' ]
```