2017 © Pedro Peláez
 

symfony-bundle exporter-bundle

Symfony ExporterBundle

image

idci/exporter-bundle

Symfony ExporterBundle

  • Thursday, October 3, 2013
  • by idciconsulting
  • Repository
  • 3 Watchers
  • 3 Stars
  • 16,790 Installations
  • PHP
  • 3 Dependents
  • 3 Suggesters
  • 2 Forks
  • 2 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

ExporterBundle

Symfony2 exporter bundle, (*1)

Installation

To install this bundle please follow the next steps:, (*2)

First add the dependency in your composer.json file:, (*3)

"require": {
    ...
    "idci/exporter-bundle": "dev-master"
},

Then install the bundle with the command:, (*4)

php composer update

Enable the bundle in your application kernel:, (*5)

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new IDCI\Bundle\ExporterBundle\IDCIExporterBundle(),
    );
}

Now the Bundle is installed., (*6)

How to use

First declare which entities can be exported in your config.yml:, (*7)

idci_exporter:
    entities:
       entity_reference:
            class: "My\\Entity\\Namespaced\\Name"

By default, the export use twig engine to create exported files. We will see later that you can create your own transformer. For the moment create a directory in YourBundle/Resources/exporter/EntityName. Then create the needed templates which convert your entity into a specific format (csv, ics, json, xml for the moment) like this, (*8)

  • export.csv.twig
  • export.ics.twig
  • export.json.twig
  • export.xml.twig

Within your template you should usually use the twig variable entity such as {{ entity.id }}., (*9)

Finaly simply use the service as follow to export your entities into the given format:, (*10)

$export = $this->container->get('idci_exporter.manager')->export($entities, $format);

This will return an Export object which contain the exported data., (*11)

$export->getContent();

To get the mime type format, (*12)

$export->getContentType();

Advanced configuration

You can customize entities transformers for each format. By default, idci_exporter.transformer_twig service is used to transform you entity. To use an other tranformer, create your own service which implements TransformerInterface.php and then associated it with a specific format like this:, (*13)

idci_exporter:
    entities:
       entity_reference:
            class: "My\\Entity\\Namespaced\\Name"
            formats:
                json:
                    transformer:
                        service: "myTransformerServiceName"

If you want to change the TwigTransformer template path or the template name, you can use specified some transformer options:, (*14)

idci_exporter:
    entities:
       entity_reference:
            class: "My\\Entity\\Namespaced\\Name"
            formats:
                json:
                    transformer:
                        service: "myTransformerServiceName"
                        options:
                            template_path: "my/new/template/path"
                            template_name_format: "myFormat.%s.ext"

By default template path is looking in the entity bundle dir Resources/exporter/EntityName/. And the template name format looks like export.%s.twig with %s replaced by the format (ex: xml, json, csv, ...)., (*15)

How to export in jsonp format

If you already have a json export, it will be very easy to export into jsonp format. Simply add a new format and set the template_name options to export.json.twig:, (*16)

So just add theses in your config.yml:, (*17)

idci_exporter:
    entities:
        entity_reference:
            class: "My\\Entity\\Namespaced\\Name"
            formats:
                jsonp:
                    transformer:
                        options:
                            template_name_format: "export.json.twig"

Use the API

This bundle help you to get your entities in a given format via HTTP requests. To do that, add its controller in the app/config/routing.yml like this:, (*18)

idci_exporter:
    resource: "../../vendor/idci/exporter-bundle/IDCI/Bundle/ExporterBundle/Controller"
    type:     annotation

Then you will be able to send request to the following routes:, (*19)

exporter_api_norewrite            ANY    /api/query
exporter_api                      ANY    /api/{entity_reference}.{_format}

{entity_reference} is a required parameter. This is the value which is define in your config.yml:, (*20)

idci_exporter:
    entities:
        entity_reference:
            class: "My\\Entity\\Namespaced\\Name"

If you get a UndefinedExportableEntityException this mean that you don't have well defined the export config for your Entity (The url might not match the entity_reference parameter for example)., (*21)

Then you have to create an extract function in your EntityRepository which return a DoctrineCollection. This function get $params variable as arguments which can be used to filters your results., (*22)

Here is an example:, (*23)

/**
 * extractQueryBuilder
 *
 * @param array $params
 * @return QueryBuilder
 */
public function extractQueryBuilder($params)
{
    $qb = $this->createQueryBuilder('cer');

    if(isset($params['id'])) {
        $qb
            ->andWhere('cer.id = :id')
            ->setParameter('id', $params['id'])
        ;
    }

    if(isset($params['category_id'])) {
        $qb
            ->leftJoin('cer.categories', 'c')
            ->andWhere('c.id = :cat_id')
            ->setParameter('cat_id', $params['category_id'])
        ;
    }

    if(isset($params['category_ids'])) {
        $qb
            ->leftJoin('cer.categories', 'cs')
            ->andWhere($qb->expr()->in('cs.id', $params['category_ids']))
        ;
    }

    return $qb;
}

/**
 * extractQuery
 *
 * @param array $params
 * @return Query
 */
public function extractQuery($params)
{
    $qb = $this->extractQueryBuilder($params);

    return is_null($qb) ? $qb : $qb->getQuery();
}

/**
 * extract
 *
 * @param array $params
 * @return DoctrineCollection
 */
public function extract($params)
{
    $q = $this->extractQuery($params);

    return is_null($q) ? array() : $q->getResult();
}

Now you can query your entities like this:, (*24)

http://mydomaine/api/query?entityReference=my_entity_reference&format=xml

or, (*25)

http://mydomaine/api/my_entity_reference.xml

The Versions

03/10 2013

dev-master

9999999-dev

Symfony ExporterBundle

  Sources   Download

GPL-3.0+

The Requires

 

csv twig json xml ics exporter

29/08 2013

1.0.x-dev

1.0.9999999.9999999-dev

Symfony ExporterBundle

  Sources   Download

GPL-3.0+

The Requires

 

csv twig json xml ics exporter

29/08 2013

v1.0.0

1.0.0.0

Symfony ExporterBundle

  Sources   Download

GPL-3.0+

The Requires

 

csv twig json xml ics exporter