murph-doc/docs/entities/em.md

131 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2021-05-31 16:30:46 +02:00
# Entity Manager
The entity manager of Muprh is a proxy of the Doctrine's entity manager. It gives you an easy way to create, update and delete an entity and dispatches events easy to subscribe to.
## Implementation
Entities must implements `App\Core\Entity\EntityInterface`.
2023-02-09 21:50:06 +01:00
```php-inline title="src/Entity/MyEntity.php"
2021-05-31 16:30:46 +02:00
namespace App\Entity;
use App\Repository\MyEntityRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Core\Entity\EntityInterface;
2023-03-10 16:29:39 +01:00
#[ORM\Entity(repositoryClass: MyEntityRepository::class)]
2021-05-31 16:30:46 +02:00
class MyEntity implements EntityInterface
{
// ...
}
```
## Usage
There are 2 entity managers which are services:
* `App\Core\Manager\EntityManager` used for all entities
* `App\Core\Manager\TranslatableEntityManager` used for translatable entities
2023-02-09 21:50:06 +01:00
```php-inline title="src/Controller/FooController.php"
2021-05-31 16:30:46 +02:00
namespace App\Controller;
use App\Core\Manager\EntityManager
use App\Entity\MyEntity;
use Symfony\Component\HttpFoundation\Response;
class FooController
{
public function foo(EntityManager $entityManager): Response
{
$myEntity = new MyEntity();
// Creates an entity
$entityManager->create($myEntity);
// Updates an entity
$entityManager->update($myEntity);
// Deletes an entity
$entityManager->delete($myEntity);
// ...
}
}
```
## Events
Events are dispatched before and after creation, update and deletion. All entities of Muprh use the entity manager.
2023-02-09 21:50:06 +01:00
```php-inline title="src/EventSubscriber/MyEntityEventSubscriber.php"
2022-04-26 15:02:26 +02:00
namespace App\EventSubscriber;
2021-05-31 16:30:46 +02:00
use App\Core\Entity\EntityInterface;
use App\Core\Event\EntityManager\EntityManagerEvent;
2022-12-13 18:12:41 +01:00
use App\Core\EventSubscriber\EntityManagerEventSubscriber;
2021-05-31 16:30:46 +02:00
use App\Entity\MyEntity;
class MyEntityEventSubscriber extends EntityManagerEventSubscriber
{
2022-04-26 15:02:26 +02:00
public function supports(EntityInterface $entity): bool
2021-05-31 16:30:46 +02:00
{
return $entity instanceof MyEntity;
}
public function onCreate(EntityManagerEvent $event)
{
2022-04-26 15:02:26 +02:00
if (!$this->supports($event->getEntity())) {
2021-05-31 16:30:46 +02:00
return;
}
// ...
}
public function onUpdate(EntityManagerEvent $event)
{
2022-04-26 15:02:26 +02:00
if (!$this->supports($event->getEntity())) {
2021-05-31 16:30:46 +02:00
return;
}
// ...
}
public function onDelete(EntityManagerEvent $event)
{
2022-04-26 15:02:26 +02:00
if (!$this->supports($event->getEntity())) {
2021-05-31 16:30:46 +02:00
return;
}
// ...
}
public function onPreCreate(EntityManagerEvent $event)
{
2022-04-26 15:02:26 +02:00
if (!$this->supports($event->getEntity())) {
2021-05-31 16:30:46 +02:00
return;
}
// ...
}
public function onPreUpdate(EntityManagerEvent $event)
{
2022-04-26 15:02:26 +02:00
if (!$this->supports($event->getEntity())) {
2021-05-31 16:30:46 +02:00
return;
}
// ...
}
public function onPreDelete(EntityManagerEvent $event)
{
2022-04-26 15:02:26 +02:00
if (!$this->supports($event->getEntity())) {
2021-05-31 16:30:46 +02:00
return;
}
// ...
}
}
```