getIdPropertyName(); $filePropertyName = $entity->getUploadedFilePropertyName(); $fileToUpload = $propertyAccessor->getValue($entity, $filePropertyName); $hasFileToUploadOnCreation = ( null === $propertyAccessor->getValue($entity, $idPropertyName) && // new object null !== $fileToUpload // has a file to upload ); if ($hasFileToUploadOnCreation) { $propertyAccessor->setValue($entity, $filePropertyName, null); } $repository->save( $entity, $hasFileToUploadOnCreation ? true // here we need to flush if we want to have access to the generated ID : $flush ); if ($hasFileToUploadOnCreation) { $propertyAccessor->setValue($entity, $filePropertyName, $fileToUpload); $vichUploaderManager->upload($entity, $entity->getUploadedFilePropertyName()); $repository->save($entity, $flush); } } }