clickandmortar / import-bundle
从文件(.csv、.xml等)导入数据
1.0.1
2019-02-28 17:08 UTC
Requires
- php: >=5.3.9
- doctrine/doctrine-bundle: ~1.4
- doctrine/orm: ^2.4.8
- symfony/symfony: 2.8.*
This package is auto-updated.
Last update: 2024-08-29 04:31:05 UTC
README
导入包可用于从平面文件(.csv、.xml等)填充实体
安装
下载包
$ composer require clickandmortar/import-bundle
启用包
通过将包添加到项目中的app/AppKernel.php
文件中注册的包列表来启用包
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... 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 }
这就完成了!