我有SonataAdminBundle 2.4.*@dev版本的后端。
在更新版本和从2.7到2.8的symfony核心之前,我的代码工作过。
解释:我有一个订阅者列表,其中有一个标志isNew,用于新用户或旧用户的单独导出。默认为1,在导出时,如果列表中存在新用户,则需要将其更改为0。
但现在不管用了。因为如果此字段设置了网格筛选器isNew并导出,则在DB字段中更改之前和以后。
return $this->get('sonata.admin.exporter')->getResponse(
$format,
$filename,
$this->admin->getDataSourceIterator()
);getDataSourceIterator从DB获取数据,而不是从结果中获取数据。这样就没有新用户了,文件也是空的。
出口后如何更新数据,有什么想法吗?
更新:
出口职能:
/**
* 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()
);
}发布于 2016-02-18 10:52:35
好的,我找到了一个解决办法,也许有人知道的更好,请在这里发帖。我通过在侦听器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;
}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());
}
}和更新服务中的数据:
/**
* @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();
}https://stackoverflow.com/questions/35452079
复制相似问题