Symfony 2 更新导出数据

Symfony 2 update data on export

我在 SonataAdminBundle 2.4 上有后端。*@dev 版本。

在将版本和 symfony 核心从 2.7 更新到 2.8 之前,我的代码有效。

解释: 我有一个订阅者列表,这些订阅者有一个标志 isNew 用于单独导出新用户或旧用户。默认为 1,如果列表中存在新用户,导出时需要将其更改为 0。

但是现在不行了。因为如果grid的filter是通过这个字段isNew和export设置的,之前DB中的字段是改的,后面是

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

getDataSourceIterator 从数据库而不是结果中获取数据。所以没有更多的新用户,文件是空的。

如何在导出后更新数据,知道吗?

更新:

导出函数:

/**
 * 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()
    );
}

好的,我找到了解决办法,也许有人知道,请 post 这里。我通过在侦听器 onTerminate 中更新数据来解决。看代码:

服务配置:

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 }

在控制器中,我删除了覆盖的方法 exportAction() 并添加了一些方法

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

导出订阅监听器:

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());
    }
}

和服务中的数据更新:

/**
 * @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();
}