requireRole($this->security, 'ROLE_GESTIONNAIRE'); $entityType = strtolower($entityType); if (!in_array($entityType, self::ALLOWED_TYPES, true)) { $this->mcpError('validation', "entityType must be one of: machine, composant, piece, product. Got '{$entityType}'."); } $entityClass = match ($entityType) { 'machine' => Machine::class, 'composant' => Composant::class, 'piece' => Piece::class, 'product' => Product::class, }; $entity = $this->em->getRepository($entityClass)->find($entityId); if (null === $entity) { $this->mcpError('not_found', ucfirst($entityType)." not found: {$entityId}"); } $results = []; foreach ($fields as $fieldEntry) { $customFieldId = $fieldEntry['customFieldId'] ?? null; $value = $fieldEntry['value'] ?? ''; if (null === $customFieldId) { $this->mcpError('validation', 'Each field entry must have a customFieldId.'); } $customField = $this->em->getRepository(CustomField::class)->find($customFieldId); if (null === $customField) { $this->mcpError('not_found', "CustomField not found: {$customFieldId}"); } $existing = $this->em->getRepository(CustomFieldValue::class)->findOneBy([ 'customField' => $customField, $entityType => $entity, ]); if (null !== $existing) { $existing->setValue((string) $value); $results[] = [ 'id' => $existing->getId(), 'customFieldId' => $customField->getId(), 'value' => (string) $value, 'action' => 'updated', ]; } else { $cfv = new CustomFieldValue(); $cfv->setCustomField($customField); $cfv->setValue((string) $value); $setter = 'set'.ucfirst($entityType); $cfv->{$setter}($entity); $this->em->persist($cfv); $this->em->flush(); $results[] = [ 'id' => $cfv->getId(), 'customFieldId' => $customField->getId(), 'value' => (string) $value, 'action' => 'created', ]; } } $this->em->flush(); return $this->jsonResponse([ 'entityType' => $entityType, 'entityId' => $entityId, 'results' => $results, 'total' => count($results), ]); } }