sndsabin / import-bundle
导入包
v0.1.1
2023-01-09 07:38 UTC
Requires
- cocur/slugify: ^4.1
- pimcore/pimcore: ^10.0
README
一个用于导入文件的具有意见的pimcore包。
支持
- CSV
- JSON
安装
先决条件
需要 pimcore >=10.0.0
步骤 1(安装包)
composer require sndsabin/import-bundle
步骤 2(启用包)
bin/console pimcore:bundle:enable ImportBundle
示例用例:导入
假设需要将 客户 记录导入到 客户数据对象类,数据来自 customer.csv。
步骤 3(添加映射器)
<?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 了解如何使用 IdentifierType::CONDITIONAL 和 IdentifierType::CONDITIONAL_PARAM 的更多示例。
步骤 4(添加配置)
为要导入的 类 配置 import.yaml
(例如:在本例中为 customer
)
#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 和 CommandConfigValidator.php 了解如何解析和验证这些属性。
@see 示例配置文件 了解所有可配置的属性。
步骤 5(使用命令导入)
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
示例用法(对于 customer 类)
bin/console data:import -c customer