php - 搜索结果未反映在输出 CSV 中
问题描述
将应用程序从 Symfony 2.8 更新到 3.0,我一直在响应更改_format
并@Template()
在下面发布。但是,虽然可以输出 CSV,但不会反映搜索结果,而是输出整个 DB 数据。最初,我只想获取 and 指定范围内的publishDateFrom
数据publishDateTo
。我是否在更新期间忽略了某些内容?
Symfony2.8->3.0:请求格式覆盖方法
如何使用@Template() 引用多个树枝
控制器
/**
* @Route("/{_format}", defaults={"_format"="html"}, requirements={"_format"="html|csv"})
* @Method("GET")
*/
public function indexAction(Request $request, $_format)
{
$searchForm = $this->createForm(new PostSearchType(), null, array(
'action' => $this->generateUrl('app_hq_post_index'),
));
$params = $this->getSearchParameter($searchForm, $request);
if ($request->getRequestFormat() == 'html') {
$count = $this->get('app.postService')->countPostBySearchParams(
$params
);
} elseif ($request->getRequestFormat() == 'csv') {
$request->attributes->set('filename', 'post_article.csv');
}
$postMetrics = $this->get('app.postService')->getPostMetrics(
$params,
$pagination ? $pagination->getItemsPerPage() : null,
$pagination ? $pagination->getSelectedPageOffset() : null
);
return $this->render('@AppBundle/Hq/Post/index.' . $_format . '.twig', [
'searchForm' => $searchForm->createView(),
'postMetrics' => $postMetrics,
'pagination' => $pagination,
'no' => $no
]);
索引.csv.twig
{% autoescape false %}
Output conditions
{% if searchForm.publishDateFrom.vars.data -%}
Access date (From),{{ searchForm.publishDateFrom.vars.data|date('Y/m/d') }}
{% endif %}
{% if searchForm.publishDateTo.vars.data -%}
Access date (To),{{ searchForm.publishDateTo.vars.data|date('Y/m/d') }}
{% endif %}
No,Count,Date
{% for metrics in postMetrics -%}
{% set article = metrics.article %}
{{ loop.index }},...
{% endfor %}
{% endautoescape %}
index.html.twig
{% block contentBody %}
{# Search form #}
<div class="search">
{{ form_start(searchForm) }}
{% set params = app.request.query.all|merge({'page': null}) %}
{% set q = app.request.query.get("q")|default({}) %}
<table>
<tr>
<td>{{ form_label(searchForm.publishDateFrom, "Posted date") }}</td>
<td>
{{ form_widget(searchForm.publishDateFrom) }}
~
{{ form_widget(searchForm.publishDateTo) }}
</td>
</tr>
<tr>
<td></td>
<td>
{# Search button #}
<button type="submit" class="btn">
<i class="icon-search"></i>Search
</button>
{# CSV output button #}
<a type="submit" class="btn" href="{{ path('app_hq_post_index', {'_format': 'csv'}) }}>
<i class="icon-download"></i>
output
</a>
</td>
</tr>
</table>
{{ form_end(searchForm) }}
</div>
服务
public function getPostMetrics(array $params = null, $limit = null, $offset = null)
{
if (!isset($params['publishDateFrom'])) {
$params['publishDateFrom'] = new \DateTime(date('Y-m-d', 0));
}
if (!isset($params['publishDateTo'])) {
$params['publishDateTo'] = new \DateTime('today');
}
$params["articleType"] = $this->targetArticleType;
$params["articleStatus"] = 'publish';
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder
->select(array(
"a as article",
"count(a) as totalCount",
"sum(case when a instance of AppBundle:Coordinate then 1 else 0 end) as Coordinate",
"sum(case when a instance of AppBundle:Recommend then 1 else 0 end) as Recommend",
"sum(case when a instance of AppBundle:Diary then 1 else 0 end) as Diary",
"sum(case when a instance of AppnBundle:ShopEvent then 1 else 0 end) as ShopEvent",
))
->from('AppBundle:Article', 'a')
->where('a.publishDateTime between :fromDate and :toDate')
->andWhere('a.shop IS NOT NULL')
->groupBy('a.shop')
->orderBy('totalCount', 'desc');
$queryBuilder->setParameter('fromDate', $params['publishDateFrom']);
$queryBuilder->setParameter('toDate', $params['publishDateTo']);
if ($limit !== null) {
$queryBuilder->setMaxResults($limit);
}
if ($offset !== null) {
$queryBuilder->setFirstResult($offset);
}
$this->entityManager->getRepository('AppBundle:Article')->addSearchParams($queryBuilder, $params);
return $queryBuilder->getQuery()->getResult();
}
调试细节
- 按特定日期搜索-> 搜索成功
- 放入
var_dump($params['publishDateFrom'])
PostService-> 显示搜索条件中指定的日期。日期的格式与更新前的格式相同。 - 将搜索结果输出到 csv-> 获取在日期搜索的数据,
new \DateTime(date('Y-m-d', 0)
而不是搜索条件中指定的日期(1970-01-01 00:00:00)。 {% if searchForm.publishDateFrom.vars.data -%}
index.csv.twig-> 中的注释Access date (From),{{searchForm.publishDateFrom.vars.data|date('Y/m/d') }}
不显示- 在 index.csv.twig 中注释掉
{% if searchForm.publishDateFrom.vars.data -%}
-> 输出今天的日期。
解决方案
推荐阅读
- optimization - SCIP符号的作用是什么?
- php - 如何在 PHP 中使用带有 SimpleXML 的数组仅显示 XML 文件中的特定数据?
- rust - 泛型加动态调度
- c - 魔术 8 球中的循环有一个错误
- javascript - 工作箱服务工作者 + manifest.json 设置 webpack 4
- python - 在 Mac 上安装 OpenCV 4.0 Alpha
- javascript - 将XML转换为CSV时如何使用knockoutJS?
- xslt - XSLT 2.0 / XPATH 测试特定位置的父元素
- android - 安卓音乐应用
- python - Python在运行时从另一个模块更改函数的局部变量