API Platform's default sub-resource POST was replacing instead of adding. Custom processor with read:false + Link + manual persist fixes this. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\Entity\Environment;
|
|
use App\Repository\ApplicationRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
final readonly class EnvironmentCreateProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private ApplicationRepository $applicationRepository,
|
|
private EntityManagerInterface $entityManager,
|
|
) {}
|
|
|
|
/**
|
|
* @param Environment $data
|
|
*/
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Environment
|
|
{
|
|
$slug = $uriVariables['slug']
|
|
?? $context['request']?->attributes->get('slug')
|
|
?? $context['request']?->attributes->get('_route_params')['slug']
|
|
?? '';
|
|
|
|
$application = $this->applicationRepository->findOneBy(['slug' => $slug]);
|
|
|
|
if (null === $application) {
|
|
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
|
}
|
|
|
|
$data->setApplication($application);
|
|
|
|
$this->entityManager->persist($data);
|
|
$this->entityManager->flush();
|
|
|
|
return $data;
|
|
}
|
|
}
|