jgrygierek / sonata-batch-entity-import-bundle
为 Sonata Admin 导入实体,带有预览和编辑功能。
Requires
- php: >=8.1.0
- jgrygierek/batch-entity-import-bundle: ^3.2.1
- sonata-project/admin-bundle: ^4.31.0
- symfony/framework-bundle: ^5.4|^6.0|^7.0
- symfony/templating: ^5.4|^6.0|^7.0
Requires (Dev)
- doctrine/doctrine-bundle: ^2.4
- friendsofphp/php-cs-fixer: ^3.62.0
- matthiasnoback/symfony-dependency-injection-test: ^4.3.1
- phpdocumentor/reflection-docblock: *
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.9.2
- phpstan/phpstan-doctrine: ^1.3
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-symfony: ^1.2
- rector/rector: ^0.15.0
- roave/security-advisories: dev-latest
- sonata-project/doctrine-orm-admin-bundle: ^4.2
- symfony/browser-kit: ^5.4|^6.0|^7.0
- symfony/dom-crawler: ^5.4|^6.0|^7.0
- symfony/phpunit-bridge: ^5.4|^6.0|^7.0
README
此包建立在 BatchEntityImportBundle 之上。
为 Sonata Admin 导入实体,带有预览和编辑功能。
- 数据在保存到数据库前可以进行查看和编辑。
- 支持 插入 新记录和 更新 已存在的记录。
- 支持的扩展:CSV, XLS, XLSX, ODS。
- 支持从 KnpLabs Translatable 扩展进行翻译。
- 代码被分割成更小的方法,便于替换以进行修改。
- 列名是必需的,并且应作为标题(第一行)添加。
- 如果列没有提供名称,将不会被加载到数据中。
文档
安装
通过 composer 安装包
composer require jgrygierek/sonata-batch-entity-import-bundle
将条目添加到 bundles.php
文件中
JG\SonataBatchEntityImportBundle\SonataBatchEntityImportBundle::class => ['all' => true],
配置类
为了定义导入功能的工作方式,您需要创建一个配置类。
基本配置类
在最简单的情况下,它将只包含使用的实体类。
namespace App\Model\ImportConfiguration; use App\Entity\User; use JG\BatchEntityImportBundle\Model\Configuration\AbstractImportConfiguration; class UserImportConfiguration extends AbstractImportConfiguration { public function getEntityClassName(): string { return User::class; } }
然后将其注册为一个服务
services: App\Model\ImportConfiguration\UserImportConfiguration: ~
字段定义
如果您想更改渲染字段的类型,而不是使用默认的,您必须在您的导入配置中覆盖该方法。如果字段名包含空格,则应使用下划线代替。
为了避免在数据导入期间出现错误,您可以在此处添加验证规则。
use JG\BatchEntityImportBundle\Model\Form\FormFieldDefinition; use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Validator\Constraints\Length; public function getFieldsDefinitions(): array { return [ 'age' => new FormFieldDefinition( IntegerType::class, [ 'attr' => [ 'min' => 0, 'max' => 999, ], ] ), 'name' => new FormFieldDefinition(TextType::class), 'description' => new FormFieldDefinition( TextareaType::class, [ 'attr' => [ 'rows' => 2, ], 'constraints' => [new Length(['max' => 255])], ] ), ]; }
矩阵验证
此包提供了两个新的验证器。
- DatabaseEntityUnique 验证器可以用来检查记录数据是否尚未存在于数据库中。
- MatrixRecordUnique 验证器可以用来检查重复,无需检查数据库,只需检查矩阵记录值。
字段名称应与上传文件中的列名称相同。只有一个例外!如果名称包含空格,则应使用下划线代替。
use JG\BatchEntityImportBundle\Validator\Constraints\DatabaseEntityUnique; use JG\BatchEntityImportBundle\Validator\Constraints\MatrixRecordUnique; public function getMatrixConstraints(): array { return [ new MatrixRecordUnique(['fields' => ['field_name']]), new DatabaseEntityUnique(['entityClassName' => $this->getEntityClassName(), 'fields' => ['field_name']]), ]; }
将服务传递给配置类
如果您想传递一些额外的服务到您的配置中,只需覆盖构造函数。
public function __construct(EntityManagerInterface $em, TestService $service) { parent::__construct($em); $this->testService = $service; }
然后您需要将此配置类定义为公开服务。
显示/隐藏实体覆盖列
如果您想隐藏/显示允许您覆盖实体 default: true
的实体列,您必须在您的导入配置中覆盖此方法。
public function allowOverrideEntity(): bool { return true; }
优化查询
如果您使用 KnpLabs Translatable 扩展对实体进行操作,您可能会注意到查询数量的增加,因为 Lazy Loading。
为了优化这一点,您可以使用 getEntityTranslationRelationName()
方法将关系名称传递给翻译。
public function getEntityTranslationRelationName(): ?string { return 'translations'; }
创建管理员
您的管理员类应该实现 AdminWithImportInterface
并包含一个额外的方法。
namespace App\Admin; use App\Model\ImportConfiguration\UserImportConfiguration; use JG\SonataBatchEntityImportBundle\Admin\AdminWithImportInterface; use Sonata\AdminBundle\Admin\AbstractAdmin; class UserAdmin extends AbstractAdmin implements AdminWithImportInterface { public function getImportConfigurationClassName(): string { return UserImportConfiguration::class; } }
默认控制器
如果您使用默认控制器,不需要任何操作。控制器将被自动替换。
自定义控制器
如果您使用自己的自定义控制器,请记住,此控制器应
- 扩展
JG\SonataBatchEntityImportBundle\Controller\ImportCrudController
- 或使用
JG\SonataBatchEntityImportBundle\Controller\ImportControllerTrait
。
此外,如果您想自动注入导入配置类,请记得实现JG\SonataBatchEntityImportBundle\Controller\ImportConfigurationAutoInjectInterface
并使用默认控制器中的getImportConfiguration()
方法。
翻译
此包支持KnpLabs的可翻译行为。
要使用此功能,每个具有可翻译值的列都应该以区域设置后缀结尾,例如
name:en
description:pl
title:ru
如果后缀添加到不可翻译的实体,则将跳过该字段。
如果后缀添加到可翻译实体,但翻译类中找不到该字段,则将跳过该字段。
覆盖模板
您有两种方法可以全局覆盖模板
- 配置 - 只需更改配置文件中模板的路径。本示例中的值是默认值,如果未更改,将使用这些值。
sonata_batch_entity_import: templates: select_file: '@SonataBatchEntityImport/select_file.html.twig' edit_matrix: '@SonataBatchEntityImport/edit_matrix.html.twig' button: '@SonataBatchEntityImport/button.html.twig'
- 包目录 - 将您的模板放在此目录中
templates/bundles/SonataBatchEntityImportBundle