Download the PHP package jgrygierek/sonata-batch-entity-import-bundle without Composer
On this page you can find all versions of the php package jgrygierek/sonata-batch-entity-import-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sonata-batch-entity-import-bundle
SonataBatchEntityImportBundle
Bundle is built on top of BatchEntityImportBundle.
Importing entities with preview and edit features for Sonata Admin.
- Data can be viewed and edited before saving to database.
- Supports inserting new records and updating existing ones.
- Supported extensions: CSV, XLS, XLSX, ODS.
- Supports translations from KnpLabs Translatable extension.
- The code is divided into smaller methods that can be easily replaced if you want to change something.
- Columns names are required and should be added as header (first row).
- If column does not have name provided, will be removed from loaded data.
Documentation
- Installation
- Configuration class
- Basic configuration class
- Fields definitions
- Matrix validation
- Passing services to configuration class
- Show & hide entity override column
- Optimizing queries
- Creating admin
- Default Controller
- Custom Controller
- Translations
- Overriding templates
- Importing data to array field
- Full example of CSV file
Installation
Install package via composer:
Add entry to bundles.php
file:
Configuration class
To define how the import function should work, you need to create a configuration class.
Basic configuration class
In the simplest case it will contain only class of used entity.
Then register it as a service:
Fields definitions
If you want to change types of rendered fields, instead of using default ones, you have to override method in your import configuration. If name of field contains spaces, you should use underscores instead.
To avoid errors during data import, you can add here validation rules.
Matrix validation
This bundle provides two new validators.
1) DatabaseEntityUnique validator can be used to check if record data does not exist yet in database. 2) MatrixRecordUnique validator can be used to check duplication without checking database, just only matrix records values.
Names of fields should be the same as names of columns in your uploaded file. With one exception! If name contains spaces, you should use underscores instead.
Passing services to configuration class
If you want to pass some additional services to your configuration, just override constructor.
Then you will need to define this configuration class as a public service too.
Show & hide entity override column
If you want to hide/show an entity column that allows you to override entity default: true
,
you have to override this method in your import configuration.
Optimizing queries
If you use KnpLabs Translatable extension for your entity, probably you will notice increased number of queries, because of Lazy Loading.
To optimize this, you can use getEntityTranslationRelationName()
method to pass the relation name to the translation.
Creating admin
Your admin class should implement AdminWithImportInterface
and should contain one additional method.
Default controller
If you use default controller, no action is needed. Controller will be replaced automatically.
Custom controller
If you use your own custom controller, remember that this controller should:
- extend
JG\SonataBatchEntityImportBundle\Controller\ImportCrudController
- or use
JG\SonataBatchEntityImportBundle\Controller\ImportControllerTrait
.
Additionally, if you want to automatically inject your import configuration class,
remember to implement JG\SonataBatchEntityImportBundle\Controller\ImportConfigurationAutoInjectInterface
and use method getImportConfiguration()
from default controller.
Translations
This bundle supports KnpLabs Translatable behavior.
To use this feature, every column with translatable values should be suffixed with locale, for example:
name:en
description:pl
title:ru
If suffix will be added to non-translatable entity, field will be skipped.
If suffix will be added to translatable entity, but field will not be found in translation class, field will be skipped.
Overriding templates
You have two ways to override templates globally:
-
Configuration - just change paths to templates in your configuration file. Values in this example are default ones and will be used if nothing will be changed.
- Bundle directory - put your templates in this directory:
Importing data to array field
If your entity has an array field, and you want to import data from CSV file to it, this is how you can do it.
- Default separator is set to
|
. - Only one-dimensional arrays are allowed.
- Keys are not allowed.
- IMPORTANT! There are limitations:
- There is no possibility to import array with one empty element, for example:
- ['']
- [null]
- But arrays with at least 2 such elements are allowed:
- ['', '']
- [null, null]
- ['', null]
- It is due of mapping CSV data to array:
- Empty value in CSV is equal to
[]
- If we have default separator,
|
value in CSV is equal to['', '']
Entity
- Allowed entity field types:
json
array
simple_array
Import configuration
- You have to add a field definition with a custom
ArrayTextType
type. If you skip this configuration,TextType
will be used as default. - You can set here your own separator.
CSV file
Full example of CSV file
All versions of sonata-batch-entity-import-bundle with dependencies
jgrygierek/batch-entity-import-bundle Version ^3.5.0
sonata-project/admin-bundle Version ^4.31.0
symfony/framework-bundle Version ^5.4|^6.0|^7.0
symfony/templating Version ^5.4|^6.0|^7.0