GithubHelp home page GithubHelp logo

sndsabin / pimcore-import-bundle Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 115 KB

An opinionated pimcore bundle for importing files.

License: GNU General Public License v3.0

PHP 100.00%
data-import import pimcore pimcore-bundle import-bundle csv-importer json-importer file-importer

pimcore-import-bundle's Introduction

Import Bundle - Pimcore Bundle

An opinionated pimcore bundle for importing files.

Supports

  • CSV
  • JSON

Installation

Prerequisite

Requires pimcore >=10.0.0

Step 1 (install the bundle)

composer require sndsabin/import-bundle

Step 2 (enable the bundle)

bin/console pimcore:bundle:enable ImportBundle

Example use case: Import

Let's say records of customer has to be imported to Customer DataObject Class from customer.csv.

Step 3 (add mapper)

<?php

namespace App\Mapper;

use Pimcore\Model\DataObject\Customer;
use SNDSABIN\ImportBundle\Helper\IdentifierType;
use SNDSABIN\ImportBundle\Contract\MapperInterface;

class CustomerMapper implements MapperInterface
{
    /** @var string */
    const FOLDER = 'customer'; // the folder inside which all customer data objects would be created

    /**
     * @param array $data
     * @return array
     */
    public function map(array $data): array
    {
        return [
            'folder' => self::FOLDER, // mandatory
            'class' => Customer::class, // mandatory
            'identifier' => [ // analogous to primary key: used for update operation (mandatory) 
                'attribute' => 'code',
                'value' => $data['Code'],
                'type' => IdentifierType::NON_CONDITIONAL
            ],
            'attributes' => [
                'code' => $data['Code'],
                'firstname' => $data['First Name'],
                'lastname' => $data['Last Name'],
                'email' => $data['Email'],
                'company' => $data['Company'],
                'address' => $data['Address'],
                'country' => $data['Country'],
                'phone' => $data['Phone'],
                'acceptsMarketing' => (bool) $data['Accepts Marketing'],
                'key' => "{$data['Code']}-{$data['First Name']}", // o_key (mandatory)
                'localisedField' => [
                    [
                        'attribute' => 'note',
                        'value' => $data['Note English'],
                        'language' => 'en'
                    ],
                    [
                        'attribute' => 'note',
                        'value' => $data['Note Nepali'],
                        'language' => 'ne'
                    ]
                ]
            ]
        ];

    }
}

@see CustomerMapper.md for more examples on how to use IdentifierType::CONDITIONAL and IdentifierType::CONDITIONAL_PARAM.

Step 4 (add configuration)

configure import.yaml for the class (Example: customer in this case) you wish to import

#config/packages/import.yaml
import:
    config:
        base_directory: '/var/www/html/import-data' # base directory where all the files to be imported are located (required)
        customer:
            file: 'customer.csv'  # file name (required)
            mapper: 'App\Mapper\CustomerMapper' # mapper (required)

@see CommandConfigResolver.php and CommandConfigValidator.php for more info on how these attributes are parsed and validated.

@see Sample Config File for all the configurable attributes.

Step 5 (import using command)

bin/console data:import [options]

Options:
  -c, --class=CLASS                     DataObject whose data is to be imported
  -f, --file[=FILE]                     path of the data file
      --book-keeping|--no-book-keeping  maintain the records (or do not maintain --no-book-keeping) of imported file

Example usage (for customer class)

bin/console data:import -c customer

pimcore-import-bundle's People

Contributors

krojan avatar sndsabin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

krojan

pimcore-import-bundle's Issues

Add namespace to bundle name to avoid naming collision

Currently, this bundle when installed is listed with the name ImportBundle
In our case, we already have a bundle in our project with name ImportBundle.
Use of namespaced bundle name like SndsabinImportBundle will resolve this issue.

Add support for loading relational data, object brick and field collection

Add support for loading relational data, object brick and field without preloading/creating the data in mapper
For an instance, an object is having attributes (manyToOneRelation, manyToManyRelation, objectBrick, fieldCollection etc.)

My idea would be to follow a convention over configuration for each type similar to LocalisedField (Line 90 in BaseImporter.php)
Sample code:
BaseImporter.php

        foreach ($data['attributes'] as $attribute => $value) {
            if ($attribute == 'localisedField' && is_array($value)) {
                $dataObject = $this->attachLocalisedField($value, $dataObject);
            } else if ($attribute == 'relationField') {
                //$value is scalar, it is manyToOneRelation
                //$value is array, it is manyToManyRelation
               //metadata can be passed as key-value pair for advanced relations as optional data params
                 $dataObject = $this->attachRelationField($value, $dataObject );
            } 
           else if (condition) { // array with string indexing is assumed to be object brick by default
           }
          else if (condition) { //array with numeric indexing is assumed to be fieldcollection by default
          }else {
                $setterMethod = 'set' . ucfirst($attribute);
                if (method_exists($dataObject, $setterMethod)) {
                    match ($setterMethod) {
                        'setKey' => $dataObject->$setterMethod($this->slugify->slugify($value)),
                        default => $dataObject->$setterMethod($value)
                    };
                }
            }
        }

BaseImporter can be added with procedures/functions for attaching structured data types (like LocalisedField) or subclasses or BaseImporter be created to solve this issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.