simple re-usable bundle to import CSV files
Add this bundle to your composer.json file:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/qweluke/CSVImporterBundle"
}
],
"require": {
"qweluke/CSVImporterBundle": "dev-master"
}
}
run composer update
to update your dependencies and install this bundle
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Qweluke\CSVImporterBundle\QwelukeCSVImporterBundle(),
// ...
);
}
);
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Qweluke\CSVImporterBundle\Entity\BaseImport;
/**
* @ORM\Entity()
*/
class Import extends BaseImport
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// add any other fields you will need
}
Add the following configuration to your parameters.yml
# app/config/parameters.yml
qweluke_csvimporter_import_class: AppBundle\Entity\Import #path to your entity
qweluke_csvimporter_requiredfields: # you can set which fields are required while importing. set "~" to none
- Username
- GivenName
- Surname
qweluke_csvimporter_fieldscount: # limit file to specified columns number. If no limit, set "~".
min: 2 # if min is not specified, bundle will require at least 1 column
max: 50
Enable symfony serializer in config.yml
. This is a "must" for parsing CSV into array. If you don't want to use symfony serializer, you must override ImportController::indexAction
from this bundle
# app/config/config.yml
framework:
serializer: { enable_annotations: true }
# app/config/routing.yml
qweluke_csv_importer:
resource: "@QwelukeCSVImporterBundle/Resources/config/routing.xml"
prefix: /
php app/console doctrine:schema:update --force
You can now use bundle. Go to http://yourdomain.tdl/import-csv
!
This bundle uses Twitter Bootstrap 3. You need to use it if you want to use this view.
You can import csv importer styles (only custom styles, without bootstrap) like this:
<link rel="stylesheet" href="{{ asset('bundles/qwelukecsvimporter/css/style.css') }}">
Create layout.html.twig
in your app/Resources/views/QwelukeCSVImporterBundle/
directory
{% extends '::base.html.twig' %}
{% block body %}
<div class="container">
{% for label, flashes in app.session.flashbag.all %}
{% for flash in flashes %}
<div class="alert alert-{{ label }}">{{ flash }}</div>
{% endfor %}
{% endfor %}
<div class="page-header">
<h1>Qweluke CSV Importer</h1>
<p class="lead">{{ 'pageheader.text'|trans({}, 'qwelukecsvimporter') }}</p>
</div>
{% block qweluke_csvimporter_content %}{% endblock %}
</div>
{% endblock %}
unit testing ;-)
https://raw.githubusercontent.com/qweluke/CSVImporterBundle/master/example_file.csv