*/ class InitiativeModel extends FormModel { public function getRepository(): InitiativeRepository { $repository = $this->em->getRepository(Initiative::class); \assert($repository instanceof InitiativeRepository); return $repository; } public function getPermissionBase(): string { return 'program:initiatives'; } /** * With no ID, returns a new initiative with the current user as owner. */ public function getEntity($id = null): ?Initiative { if (null === $id) { $initiative = new Initiative(); $initiative->setOwner($this->userHelper->getUser(true)); return $initiative; } $initiative = parent::getEntity($id); return $initiative instanceof Initiative ? $initiative : null; } /** * Keeps objective and description item positions contiguous (0..n-1) before saving. */ public function saveEntity($entity, $unlock = true): void { if ($entity instanceof Initiative) { $entity->normalizePositions(); } parent::saveEntity($entity, $unlock); } /** * @param array $options */ public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface { if (!$entity instanceof Initiative) { throw new MethodNotAllowedHttpException(['Initiative']); } if (!empty($action)) { $options['action'] = $action; } return $formFactory->create(InitiativeType::class, $entity, $options); } }