在会话中将过滤器存储在 LexikFormFilterBundle 中

Store filters in session in LexikFormFilterBundle

目前我在会话中存储过滤器是这样的:

// Filter action
if ('filter' == $request->get('filter_action')) {
    // Bind values from the request
    $filterForm->handleRequest($request);

    if ($filterForm->isValid()) {
        // Build the query from the given form object
        $filterUpdater->addFilterConditions($filterForm, $queryBuilder);
        // Save filter to session
        $filterData = $filterForm->getData();
        $session->set(sprintf('%sControllerFilter', $this->filterName), $filterData);
        $session->set(sprintf('%sControllerFilterPage', $this->filterName), 1);
    }
} else {
    // Get filter from session
    if ($session->has(sprintf('%sControllerFilter', $this->filterName))) {
        $filterData = $session->get(sprintf('%sControllerFilter', $this->filterName));
        foreach ($filterData as $key => $filter) {
            if (\is_object($filter)) {
                $filterData[$key] = $em->merge($filter);
            }
        }
        $filterForm = $this->createFilterForm($filterData, $this->getSiteFromSession($request));
        $filterUpdater->addFilterConditions($filterForm, $queryBuilder);
    }
}

但由于 EntityManager::merge() 弃用,我需要更改此解决方案。任何想法如何去做?解决方案是跳过使用 EntityFilterType 并使用 ChoiceFilterType 但我不想这样做,因为 EntityFilterType 是一个更舒适的解决方案。

如果我从会话中删除负责合并实体的行,则会出现错误:

Entity of type "App\Entity\Category" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?

merge 用于重新附加分离的实体。 persist告诉Doctrine要保存实体,所以实现了re-attach。这样您就可以实现实体将被管理,正如错误消息所暗示的那样。

您可以在这里阅读更多内容:https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database