Symfony 2更新导出数据

I have backend on SonataAdminBundle 2.4.*@dev version.

Before update version and symfony core from 2.7 to 2.8 my code worked.

Explain: I have an list of subscribers that have a flag isNew for separate export of new or old users. By default is 1, on export need to change it to 0 if in list exist new users.

But now it doesn't work. Because if filter of grid is set by this field isNew and export, in DB field is changed before, and later

return $this->get('sonata.admin.exporter')->getResponse(
        $format,
        $filename,
        $this->admin->getDataSourceIterator()
    );

getDataSourceIterator take data from DB not from result. So there is no more new users and file is empty.

How to update data after export, have any idea?

UPDATE:

Export function:

/**
 * Export data to specified format.
 *
 * @param Request $request
 *
 * @return Response
 *
 * @throws AccessDeniedException If access is not granted
 * @throws \RuntimeException     If the export format is invalid
 */
public function exportAction(Request $request = null)
{
    $request = $this->resolveRequest($request);

    $this->admin->checkAccess('export');

    $format = $request->get('format');

    $allowedExportFormats = (array) $this->admin->getExportFormats();

    if (!in_array($format, $allowedExportFormats)) {
        throw new \RuntimeException(
            sprintf(
                'Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`',
                $format,
                $this->admin->getClass(),
                implode(', ', $allowedExportFormats)
            )
        );
    }

    $filename = sprintf(
        'export_%s_%s.%s',
        strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
        date('Y_m_d_H_i_s', strtotime('now')),
        $format
    );


    //my code to update field isNew of subscribers
    $this->get('cfw.subscription')->processExportEmails($controller->admin->getFilterParameters());

    return $this->get('sonata.admin.exporter')->getResponse(
        $format,
        $filename,
        $this->admin->getDataSourceIterator()
    );
}

Ok, I found an solution, maybe someone know better, please post here. I solved by update data in listener onTerminate. Look code:

service config:

sonata.admin.subscription.listener:
        class: SiteBundle\Listener\ExportSubscriptionListener
        arguments: [@service_container]
        tags:
            - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
            - { name: kernel.event_listener, event: kernel.terminate, method: onKernelTerminate }

in controller I deleted overridden method exportAction() and added little method

/**
 * @return AdminInterface
 */
public function getAdmin()
{
    return $this->admin;
}

ExportSubscriptionListener:

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Sonata\AdminBundle\Controller\CRUDController;
use SiteBundle\Controller\SubscriptionAdminController;

class ExportSubscriptionListener
{
    /**
     * @var CRUDController
     */
    protected $controller;

    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function onKernelController(FilterControllerEvent $event)
    {
        $this->controller = $event->getController();
    }

    /**
     * Update new subscribers on export
     */
    public function onKernelTerminate()
    {
        $controller = $this->controller[0];

        if (!$controller instanceof SubscriptionAdminController) {
            return;
        }

        if ($this->controller[1] !== 'exportAction') {
            return;
        }

        //here we update in service data
        /** $var SubscriptionAdminController $controller */
        $this->container->get('service.subscription')->processExportEmails($controller->getAdmin()->getFilterParameters());
    }
}

and update of data in service:

/**
 * @param array $subscriptions
 */
public function processExportEmails(array $filterParameters)
{
    $criteria = [];

    if(!empty($filterParameters['locale']) && $filterParameters['locale']['value'] !== "") {
        $criteria['locale'] = $filterParameters['locale']['value'];
    }

    if(!empty($filterParameters['new']) && $filterParameters['new']['value'] !== "") {
        $criteria['new'] = $filterParameters['new']['value'];
    }

    $subscriptionRepository = $this->getRepositorySubscription();

    $subscriptions = null;
    if (count($criteria) > 0) {
        $subscriptions = $subscriptionRepository->findBy($criteria);
    } else {
        $subscriptions = $subscriptionRepository->findAll();
    }

    /** @var array|null $subscriptions */
    foreach ($subscriptions as $subscription) {
        /** @var Subscription $subscription */
        if ($subscription->isNew()) {
            $subscription->setNew(false);
            $this->getEntityManager()->persist($subscription);
        }
    }

    $this->getEntityManager()->flush();
}