An opinionated pimcore bundle for importing files.
- CSV
- JSON
Requires pimcore >=10.0.0
composer require sndsabin/import-bundle
bin/console pimcore:bundle:enable ImportBundle
Let's say records of customer has to be imported to Customer DataObject Class from customer.csv.
<?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.
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.
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
bin/console data:import -c customer