symfony尝试从链接导出csv文件

时间:2013-12-28 10:58:27

标签: symfony csv

我在控制器中有这个功能,它返回一个带有选择框的表单。选择值后,我将使用

检索它们
$manifestations = $form['manifestations']->getData();

然后将它们放入查询数据库$invites = $repository->searchInviteByManif($manifestations);

的存储库函数中
 public function indexAction() {

    $entity = new Invite();

    $form = $this->createForm(new ManifSearchType(), $entity);

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

    $invites = null;

    if ($request->getMethod() == 'POST') {

        $form->handleRequest($request);

        $message = '';

        $manifestations = $form['manifestations']->getData();

        $repository = $this->getDoctrine()
                ->getManager()
                ->getRepository('PrifProtocoleBundle:Invite');

        $invites = $repository->searchInviteByManif($manifestations);

        $response1 = $this->render('PrifProtocoleBundle:Invite:index.html.twig', array(
            'form' => $form->createView(),
            'entities' => $invites,
            'message' => $message,
        ));

        return $response1;

    }
    return array(
        'form' => $form->createView(),
        'entities' => $invites,
    );
}

然后,此函数返回一个视图index.html.twig,其中包含一个表以及在数据库中找到的所有字段。

我想要的是通过直接点击HTML表格中的链接,将所有查询的数据$invites导出到CSV文件中。

所以我在Twig文件中放了一个href=""链接,

{% if message is defined %}   
  <div class="pdf"> 
  <a href=""><img height="40px" width="40px" src={{ asset('bundles/prifprotocole/images/excel.jpg')   }}></a>

  {% for entity in entities %}
        <tr class="{{ cycle(['odd', 'even'], loop.index0) }}">
            <td>{% if entity.etat == 1 %} Actif {% else %} Inactif {% endif %}</td>
            <td>{{ entity.titreGrade }} {{ entity.prenom }} {{ entity.nom }}</td>
            <td>{{ entity.fonction }}</td>

这是我用来导出没有链接的CSV文件的方式:

$response2 = $this->render('PrifProtocoleBundle:Invite:export.csv.twig', array(
            'entities' => $invites));

$response2->headers->set('Content-Type', 'text/csv');

$csvfile = $response2->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
return $csvfile;

export.csv.twig档案

{% for entity in entities %}
  Id {{ entity.id }};
  Etat {{ entity.etat }};
  Titregrade {{ entity.titreGrade }};
  Prenom {{ entity.prenom }};
  Nom {{ entity.nom }};
  Fonction {{ entity.fonction }};
{% endfor %}

有人可以给我一个如何执行此操作的详细解决方案吗?非常感谢!

2 个答案:

答案 0 :(得分:0)

您应该首先通过序列化将过滤器标准(此处为$manifestations数组作为路径参数)传递给过滤器标准。

然后,您应该像downloadCsvAction()这样的控制器处理此路由,从数据库中查询$invites并呈现export.csv.twig模板。

如果无法序列​​化URI中的数据,另一个选项是创建一个包含数据的隐藏字段的表单。然后使用此隐藏表单的提交按钮替换下载链接。

答案 1 :(得分:0)

如果您不介意两次提取数据,Guillaume的答案很棒。一旦显示页面,然后下载CSV。另一种方法是缓存答案。我将使用memcache(这是我使用的)给出一个例子。

首先,确保你的php.ini中的memcache是​​活动的。

然后在渲染第一页之前(在获取数据之后),缓存它:

$key = "csv.$userId";
$memcache = new \Memcache();
$memcache->connect($yourServerHost, $yourServerPort);
$memcache->set($key, json_encode($invites), MEMCACHE_COMPRESSED, 86400); // 24h

诀窍是找到一个好的密钥,将报告添加到缓存中,然后才能检索它。它必须是独一无二的。在这里,我假设您有一个登录的用户,我使用用户ID来创建密钥。如果不是这种情况,您可以自己创建一个令牌,并在呈现页面时传递它(以及$ invites)。这样,您可以将令牌添加到URL以生成CSV。

然后,在您下载CSV的第二个操作中,您只需获取缓存的数据:

$memcache = new \Memcache();
$memcache->connect($yourServerHost, $yourServerPort);
$json = $memcache->get($key);
$invites = json_decode($json , true);

就是这样。您可能应该为Memcache创建一个服务,因此您不必创建对象,并且每次都要连接。