2lenet / import-bundle
从文件(.csv, .xml, ...)导入数据
1.4.5
2020-01-15 15:04 UTC
README
Import Bundle 可以用于从平面文件(.csv, .xml, 等)填充实体
- 安装
将包添加到你的 composer.json
文件中
"require": { ... "2lenet/import-bundle": "^1.0" ... }
运行 composer update
将捆绑包添加到你的项目中
composer update 2lenet/import-bundle
在您的 app/AppKernel.php
文件中添加捆绑包
$bundles = array( ... new ClickAndMortar\ImportBundle\ClickAndMortarImportBundle(), );
- 配置
在您的 app/config.yml 文件中使用自己的实体配置捆绑包。示例
click_and_mortar_import: entities: customer_from_pim: model: Acme\DemoBundle\Entity\Customer repository: AcmeDemoBundle:Customer unique_key: id mappings: id: "ID" name: "Name_For_Customer" firstname: "FirstName" gender: "Sex" age: "Age" ...
您可以通过简单地更改导入过程名称为单个实体定义多个导入(例如,在 entities 下添加一个新的部分,名称为 customer_from_ecommerce)
可用的选项
- 使用方法
使用命令启动文件导入
php app/console candm:import /path/of/your/file/customers.csv customer_from_pim
可用的选项
- 扩展
您可以使用自己的读取器读取其他文件类型。
创建您的类(在 YourOrganizationName/YourBundle/Reader/Readers 中)并扩展 AbstractReader
<?php namespace YourOrganizationName\YourBundle\Reader\Readers; use ClickAndMortar\ImportBundle\Reader\AbstractReader; /** * Class MyCustomXmlReader * * @package YourOrganizationName\YourBundle\Reader\Readers */ class MyCustomXmlReader extends AbstractReader { /** * Read my custom XML file and return data array * * @param string $path * * @return array */ public function read($path) { $data = array(); ... return $data; } /** * Support only xml type * * @param string $type * * @return bool */ public function support($type) { return $type == 'xml'; } }
将类声明为服务(在 YourOrganizationName/YourBundle/Resource/config/services.yml 中)并添加标签 clickandmortar.import.reader
parameters: yourorganizationname.yourbundle.reader.my_custom_reader.class: YourOrganizationName\YourBundle\Reader\Readers\MyCustomXmlReader services: yourorganizationname.yourbundle.reader.my_custom_reader: class: %yourorganizationname.yourbundle.reader.my_custom_reader.class% tags: - { name: clickandmortar.import.reader }
这就完成了!