bigfoot / import-bundle
Bigfoot 导入包
2.2.1
2014-11-24 10:47 UTC
Requires
- bigfoot/core-bundle: ~2.2
This package is auto-updated.
Last update: 2024-09-06 01:48:14 UTC
README
ImportBundle 是由 C2IS 创建的 BigFoot 框架的一部分。
支持的文件格式
- CSV
- XML
如何导入 XML 文件
安装
在 composer.json 文件的 'require' 部分添加 'BigFoot/ImportBundle'
"require": { ... ... "bigfoot/import-bundle": "dev-master", }
更新您的项目
php composer.phar update
创建您的特定导入包(例如:BigfootQualitelisBundle)
app/console generate:update
根据您的 CSV 文件的不同字段生成您的实体/实体(例如:QualitelisNote)
app/console generate:doctrine:entity
然后更新您的数据库
app/console doctrine:schema:update --force
在您的包的根目录中创建一个名为 'Services' 的目录,然后创建一个扩展 'AbstractSimpleDataMapper' 模型的类文件。
/* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { ... }
为每个要导入的字段创建一些常量。它们的值必须与 CSV 表头相同。
/* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2'; }
将常量关联到您的存储库设置器
/* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2'; protected function getColumnMap() { return array( self::FIELD_1 => 'setField1', self::FIELD_2 => 'setField2', ... ... ); } }
设置您的 CSV 文件的编码,例如 UTF8
/* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2'; protected function getColumnMap() { return array( self::FIELD_1 => 'setField1', self::FIELD_2 => 'setField2', ... ... ); } protected function getEncodedValue($value) { return utf8_encode($value); } }
在配置文件中设置导入参数
- nb_ligne_par_lot /ftp / csv = number of lines per batch
- max_execution_time = avoid the time out
# app/config/config.yml ... bigfoot_import: nb_ligne_par_lot : ftp : csv : 10 max_execution_time : 500
设置您的包的命名空间并从您的映射类创建一个服务
# Resources/config/services.yml parameters: bigfoot_qualitelis.note_datamapper.class: 'Bigfoot\Bundle\QualitelisBundle\Services\QualitelisNotesDataMapper' bigfoot_qualitelis.namespace: 'Bigfoot\Bundle\QualitelisBundle\Entity\' services: bigfoot_qualitelis.note_datamapper: class: '%bigfoot_qualitelis.note_datamapper.class%' arguments: [@service_container, '%nb_ligne_par_lot.ftp.csv%', '%bigfoot_qualitelis.namespace%']
在 'getObject' 方法中设置 ID 键(此处键为 FIELD_1)
/* Services/QualitelisNotesDataMapper.php */ class QualitelisNotesDataMapper extends AbstractSimpleDataMapper { const FIELD_1 = 'header_field_1'; const FIELD_2 = 'header_field_2'; protected function getColumnMap() { return array( self::FIELD_1 => 'setField1', self::FIELD_2 => 'setField2', ... ... ); } protected function getEncodedValue($value) { return utf8_encode($value); } protected function getObject($className, $line) { $em = $this->container->get('doctrine.orm.default_entity_manager'); $qualitelis_namespace = $this->container->getParameter('bigfoot_qualitelis.namespace'); $object = $em->getRepository($qualitelis_namespace.$className)->findOneBy(array(self::FIELD_1 => $line[$this->data->getIndexOfHead(self::FIELD_1)])); if (!$object) { $fqcn = $qualitelis_namespace.'\\Entity\\'.$className; return new $fqcn; } return $object; } }
配置
您可以在您的 config.yml
中定义可用的 Datasource 协议。默认情况下,只有 http 和 ftp 协议可用。
# app/config/config.yml bigfoot_import: datasource: protocol: ftp: FTP http: HTTP scp: SCP ssh: SSH
用法
前往位于 /admin/datasource/ 的管理界面。
添加配置(名称、协议、域名、端口、用户名、密码)。
要导入,在操作方法中写入以下内容
/* Controller/DefaultController.php */ public function indexAction() { $em = $this->container->get('doctrine.orm.default_entity_manager'); /* Where 'nameOfTheFtpConfiguration' is the name you entered for the FTP configuration */ $object = $em->getRepository('BigfootImportBundle:DataSource')->findOneBy(array('name' => 'nameOfTheFtpConfiguration')); $client = $this->get('bigfoot_import.client'); $client->init($object->getDomain()); $client->setAuth($object->getUsername(),$object->getPassword()); $parser = $this->get('bigfoot_import.csvparser'); $parser->setClient($client); $parser->setDelimiter(';'); /* Name of your csv file in the FTP */ $data = $parser->parse('nameofthecsvfile.csv'); /* Name of the service */ $dataMapper = $this->get('bigfoot_qualitelis.note_datamapper'); $dataMapper->setData($data); /* Name of your entity */ $dataMapper->className = 'QualitelisNote'; $dataMapper->map(); return new Response(); }