persistProcessor->process($data, $operation, $uriVariables, $context); } $previous = $context['previous_data'] ?? null; $parentChanged = $previous instanceof Forum && $previous->getParent()?->getId() !== $data->getParent()?->getId(); if ($data->getPosition() === 0 || $parentChanged) { $qb = $this->entityManager->createQueryBuilder(); $qb->select('COALESCE(MAX(f.position), 0)') ->from(Forum::class, 'f'); if ($data->getParent()) { $qb->andWhere('f.parent = :parent') ->setParameter('parent', $data->getParent()); } else { $qb->andWhere('f.parent IS NULL'); } $maxPosition = (int) $qb->getQuery()->getSingleScalarResult(); $data->setPosition($maxPosition + 1); } return $this->persistProcessor->process($data, $operation, $uriVariables, $context); } }