2017 © Pedro PelĂĄez
 

symfony-bundle import-bundle

Bigfoot import bundle

image

bigfoot/import-bundle

Bigfoot import bundle

  • Tuesday, January 9, 2018
  • by gmanen
  • Repository
  • 14 Watchers
  • 0 Stars
  • 12,063 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 21 Versions
  • 1 % Grown

The README.md

ImportBundle

ImportBundle is part of the framework BigFoot created by C2IS., (*1)

File formats supported : * CSV * XML, (*2)

How to import XML file

See XmlMapper Documentation, (*3)

Installation

Add 'BigFoot/ImportBundle' into your composer.json file in the 'require' section:, (*4)

``` json "require": { ... ... "bigfoot/import-bundle": "dev-master", }, (*5)


Update your project: ``` shell php composer.phar update

Create your specific import bundle (ie: BigfootQualitelisBundle):, (*6)

``` shell app/console generate:update, (*7)


Generate your entity/ies based on the different fields of your csv file (ie: QualitelisNote): ``` shell app/console generate:doctrine:entity

Then update your database:, (*8)

``` shell app/console doctrine:schema:update --force, (*9)


Create a directory 'Services' in the root directory of your bundle then create a class file which extends the model 'AbstractSimpleDataMapper'. ``` php /* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { ... }

Create some constants for each field you want to import. Their values must be the same as the header's csv., (*10)

``` php /* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2'; }, (*11)


Associate the constants to your repository setters: ``` php /* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2'; protected function getColumnMap() { return array( self::FIELD_1 => 'setField1', self::FIELD_2 => 'setField2', ... ... ); } }

Set the coding of your csv file, for instance in UTF8:, (*12)

``` php /* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2';, (*13)

protected function getColumnMap()
{
    return array(

        self::FIELD_1       => 'setField1',
        self::FIELD_2       => 'setField2',

        ...
        ...


    );
}

protected function getEncodedValue($value) {
    return utf8_encode($value);
}

}, (*14)


Set the import parameters in your config file: - nb_ligne_par_lot /ftp / csv = number of lines per batch - max_execution_time = avoid the time out ``` yml # app/config/config.yml ... bigfoot_import: nb_ligne_par_lot : ftp : csv : 10 max_execution_time : 500

Set the namespace of your bundle and create a service from your mapping class:, (*15)

``` yml, (*16)

Resources/config/services.yml

parameters: bigfoot_qualitelis.note_datamapper.class: 'Bigfoot\Bundle\QualitelisBundle\Services\QualitelisNotesDataMapper' bigfoot_qualitelis.namespace: 'Bigfoot\Bundle\QualitelisBundle\Entity\', (*17)

services: bigfoot_qualitelis.note_datamapper: class: '%bigfoot_qualitelis.note_datamapper.class%' arguments: [@service_container, '%nb_ligne_par_lot.ftp.csv%', '%bigfoot_qualitelis.namespace%'], (*18)


Set the ID key in the method 'getObject' (here the key is FIELD_1): ``` php /* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2'; protected function getColumnMap() { return array( self::FIELD_1 => 'setField1', self::FIELD_2 => 'setField2', ... ... ); } protected function getEncodedValue($value) { return utf8_encode($value); } protected function getObject($className, $line) { $em = $this->container->get('doctrine.orm.default_entity_manager'); $qualitelis_namespace = $this->container->getParameter('bigfoot_qualitelis.namespace'); $object = $em->getRepository($qualitelis_namespace.$className)->findOneBy(array(self::FIELD_1 => $line[$this->data->getIndexOfHead(self::FIELD_1)])); if (!$object) { $fqcn = $qualitelis_namespace.'\\Entity\\'.$className; return new $fqcn; } return $object; } }

Configuration

You could define availables protocols for Datasource in your config.yml. By default, only http and ftp protocols are availables., (*19)

# app/config/config.yml

bigfoot_import:
    datasource:
        protocol:
            ftp: FTP
            http: HTTP
            scp: SCP
            ssh: SSH

Usage

Go to the admin interface available at /admin/datasource/., (*20)

Add a configuration (name, protocol, domain, port, username, password)., (*21)

To import, write this into an action method:, (*22)

``` php /* Controller/DefaultController.php */, (*23)

public function indexAction() {, (*24)

$em = $this->container->get('doctrine.orm.default_entity_manager');

/* Where 'nameOfTheFtpConfiguration' is the name you entered for the FTP configuration  */
$object = $em->getRepository('BigfootImportBundle:DataSource')->findOneBy(array('name' => 'nameOfTheFtpConfiguration'));

$client = $this->get('bigfoot_import.client');
$client->init($object->getDomain());
$client->setAuth($object->getUsername(),$object->getPassword());

$parser = $this->get('bigfoot_import.csvparser');
$parser->setClient($client);
$parser->setDelimiter(';');

/* Name of your csv file in the FTP */
$data = $parser->parse('nameofthecsvfile.csv');

/* Name of the service */
$dataMapper = $this->get('bigfoot_qualitelis.note_datamapper');

$dataMapper->setData($data);

/* Name of your entity */
$dataMapper->className = 'QualitelisNote';

$dataMapper->map();

return new Response();

}, (*25)

```, (*26)

The Versions

09/01 2018

dev-master

9999999-dev

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

The Development Requires

by S.Huot
by S.Plançon

24/03 2017

3.0.x-dev

3.0.9999999.9999999-dev

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

05/04 2016

dev-fix-truefalsetrue

dev-fix-truefalsetrue

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

16/02 2016

dev-Refacto-DataManager

dev-Refacto-DataManager

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

04/02 2016

dev-transversal-data

dev-transversal-data

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

30/12 2015

dev-undeprecatization

dev-undeprecatization

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

21/05 2015

2.2.x-dev

2.2.9999999.9999999-dev

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

24/11 2014

2.2.1

2.2.1.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

20/11 2014

v2.2.0

2.2.0.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

10/07 2014

dev-stabilisation

dev-stabilisation

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

16/06 2014

2.1.x-dev

2.1.9999999.9999999-dev

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

16/06 2014

v2.1.1

2.1.1.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

16/06 2014

v2.1.0

2.1.0.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

17/02 2014

dev-content

dev-content

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

13/02 2014

1.2.1

1.2.1.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

12/02 2014

dev-refacto

dev-refacto

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

04/02 2014

1.0.x-dev

1.0.9999999.9999999-dev

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

04/02 2014

1.2.0

1.2.0.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

29/01 2014

1.1.0

1.1.0.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

21/01 2014

v1.0.0

1.0.0.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon

21/01 2014

v1.0

1.0.0.0

Bigfoot import bundle

  Sources   Download

MIT

The Requires

 

by S.Huot
by S.Plançon