I want to call the data persisters of all my child's entities from a parent entity. For example, the entity Demand has some children called ObjectStorage (medias...).
When persisting the entity A, the data persister is called, but not the child data persisters... (all OneToMany relations)
Does anyone know?
I followed this: https://api-platform.com/docs/core/data-persisters/#calling-multiple-datapersisters
Giving this to my project:
// src/Entity/DemandeTransport.php
/**
* @var Collection
*/
#[OneToMany(mappedBy: 'demandeTransport', targetEntity: ObjectStorage::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups(['read:demande:transport', 'write'])]
public Collection $images;
// src/Entity/ObjectStorage.php
/**
* @var DemandeTransport|null
*/
#[ManyToOne(targetEntity: DemandeTransport::class, inversedBy: 'images')]
#[Groups(['read', 'write'])]
public ?DemandeTransport $demandeTransport;
// src/DataPersister/DemandDataPersister.php
<?php
namespace App\DataPersister;
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use ApiPlatform\Core\DataPersister\ResumableDataPersisterInterface;
use App\Entity\DemandeTransport;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class DemandDataPersister implements ContextAwareDataPersisterInterface, ResumableDataPersisterInterface
{
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $_entityManager;
/**
* @var TokenStorageInterface
*/
private TokenStorageInterface $tokenStorage;
public function __construct(EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage)
{
$this->_entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
}
public function supports($data, array $context = []): bool
{
return $data instanceof DemandeTransport;
}
public function persist($data, array $context = [])
{
$data->setUtilisateur($this->tokenStorage->getToken()->getUser());
$this->_entityManager->persist($data);
$this->_entityManager->flush();
}
public function remove($data, array $context = [])
{
$this->_entityManager->remove($data);
$this->_entityManager->flush();
}
public function resumable(array $context = []): bool
{
return true;
}
}
// src/DataPersister/ObjectStoragePersist.php <-- NOT CALLED
<?php
namespace App\DataPersister;
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use ApiPlatform\Core\DataPersister\ResumableDataPersisterInterface;
use App\Entity\ObjectStorage;
use Doctrine\ORM\EntityManagerInterface;
class ObjectStorageDataPersister implements ContextAwareDataPersisterInterface, ResumableDataPersisterInterface
{
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $_entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->_entityManager = $entityManager;
}
public function supports($data, array $context = []): bool
{
return $data instanceof ObjectStorage;
}
public function persist($data, array $context = [])
{
dd('TEST DEBUG');
if($data->getBase64()) {
dd($data->getBase64());
}
$this->_entityManager->persist($data);
$this->_entityManager->flush();
}
public function remove($data, array $context = [])
{
$this->_entityManager->remove($data);
$this->_entityManager->flush();
}
public function resumable(array $context = []): bool
{
return true;
}
}
// services.yaml
App\DataPersister\DemandDataPersister:
decorates: 'api_platform.doctrine.orm.data_persister'
App\DataPersister\ObjectStorageDataPersister:
decorates: 'api_platform.doctrine.orm.data_persister'
Does anyone know how to call all data persisters of a parent entity?