lexik / LexikFormFilterBundle

This Symfony bundle aim to provide classes to build some form filters and then build a doctrine query from this form filter.
MIT License
389 stars 119 forks source link

Store Entity in session #327

Closed thomas2411 closed 4 years ago

thomas2411 commented 4 years ago

At the beginning I apologize I didn't use StackOverflow, but the official tag lexikformfilterbundle is not available and due to my reputation (900) I cannot create a new one.

Curenntly I store filters in session like this:

 // Filter action
        if ($request->get('filter_action') == 'filter') {
            // 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);
            }
        }

But due to EntityManager::merge() deprecations I need to change this solution. Any ideas how to do it? The solution is to skip using EntityFilterType and use ChoiceFilterType but I don't want to, cause it is a nice solution. There are some dirty solutions in mind but as said, they are dirty.

thomas2411 commented 4 years ago

Change it into a persist works perfect, so instead of $filterData[$key] = $em->merge($filter); in a loop I use $em->persist($filter);