symfony试图从链接导出csv文件 [英] symfony trying to export a csv file from a link

查看:232
本文介绍了symfony试图从链接导出csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中有这个函数,它返回一个带有选择框的窗体。当选择这些值时,我用

I have this function in the controller, that returns a form with a select box. When the values are selected, I retrieve them with

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

然后将它们放在查询数据库的存储库函数中 $ invites = $ repository-> searchInviteByManif($ manifestations);

then put them in a repository function that queries the database $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。

This function then returns a view index.html.twig with a table and all the fields found in the db.

我想要的是导出所有查询的数据

What I want is to export all the queried data $invites in a CSV file, by clicking on a link directly from the HTML table.

因此,我在CSV文件中添加了<$ c> $ invites $ c> href =在Twig文件中的链接,

So I've put an href="" link in the Twig file,

{% 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文件而没有链接的方法:

This is how I use to export the CSV file without the link :

$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 p>

export.csv.twig file

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

有人可以给我一个详细的解决方案, ?非常感谢!

Can someone give me a detailed solution on how to perform this? Much thanks!

推荐答案

Guillaume的答案是伟大的,如果你不介意获取您的数据两次。一次显示您的网页,然后下载CSV。另一种方法是缓存答案。我将使用memcache(这是我使用的)的示例。

Guillaume's answer is great if you don't mind fetching your data twice. Once when you display your page, and then when you download the CSV. Another way to do this is to cache the answer. I will give an example using memcache (which is what I use).

首先,确保memcache在您的php.ini中是活动的。

First, make sure that memcache is active in your php.ini.

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

Then before rendering your first page (after fetching the data), cache it:

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

诀窍是找到一个好的键,将您的报告添加到缓存,然后能够检索它。它必须是独一无二的。在这里,我假设您有一个已登录的用户,并且我使用用户ID来创建密钥。如果不是这样,你可以创建一个令牌,并在渲染页面(连同$ invites)时传递它。这样,您可以将令牌添加到网址中以生成CSV。

The trick is to find a good key, to add your report to the cache, and then be able to retrieve it. It has to be unique. Here, I assume that you have a user that is logged in, and I use the user ID to create a key. If that is not the case, you could create yourself a token, and pass it when rendering the page (along with $invites). This way, you can add the token to the URL to generate the CSV.

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

Then, in your second action, where you download the CSV, you just fetch the cached data:

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

就是这样。你应该为Memcache创建一个服务,所以你不必创建一个对象,并且每次都连接。

That's it. You should probably create yourself a service for Memcache, so you don't have to create an object, and connect everytime.

这篇关于symfony试图从链接导出csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆