requireRole($this->security, 'ROLE_GESTIONNAIRE'); $piece = new Piece(); $piece->setName($name); if ('' !== $reference) { $piece->setReference($reference); } if ('' !== $description) { $piece->setDescription($description); } if ('' !== $prix) { $piece->setPrix($prix); } if ('' !== $modelTypeId) { $modelType = $this->modelTypes->find($modelTypeId); if (!$modelType) { $this->mcpError('not_found', "ModelType not found: {$modelTypeId}"); } $piece->setTypePiece($modelType); } foreach ($constructeurIds as $cId) { $c = $this->constructeurs->find($cId); if (!$c) { $this->mcpError('not_found', "Constructeur not found: {$cId}"); } $piece->addConstructeur($c); } $this->em->persist($piece); $this->em->flush(); return $this->jsonResponse([ 'id' => $piece->getId(), 'name' => $piece->getName(), ]); } }