php 是否可以为自定义实体创建一个配置文件,以便使用CSV导入/导出数据?

jhkqcmku  于 2023-01-24  发布在  PHP
关注(0)|答案(2)|浏览(127)

在shopware中,可以使用配置文件导入/导出数据。请参见以下链接。https://docs.shopware.com/en/shopware-en/settings/importexport

现在唯一的问题是我想上传一个自定义实体的数据。我在配置文件中找不到自定义实体。所以我的问题是是否可以通过CSV批量上传此数据?

2cmtqfgy

2cmtqfgy1#

您必须为自定义实体保留导入/导出配置文件:

$container->get('import_export_profile.repository')->create([
    [
        'name' => 'My Custom Entity',
        'label' => 'My Custom Entity',
        'sourceEntity' => 'my_custom_entity',
        'type' => ImportExportProfileEntity::TYPE_IMPORT_EXPORT,
        'fileType' => 'text/csv',
        'delimiter' => ';',
        'enclosure' => '"',
        'config' => [],
        'mapping' => [
            ['key' => 'id', 'mappedKey' => 'id', 'position' => 1],
            ['key' => 'active', 'mappedKey' => 'active', 'position' => 2],
            ['key' => 'translations.DEFAULT.name', 'mappedKey' => 'name', 'position' => 3],
            ['key' => 'type', 'mappedKey' => 'type', 'position' => 0],
        ],
    ]
], $context);

在这里,您必须将实体字段Map到CSV的列。
如果要影响字段的写入或读取方式,可能需要为实体注册序列化程序。例如,产品实体具有序列化程序:

<service id="Shopware\Core\Content\ImportExport\DataAbstractionLayer\Serializer\Entity\ProductSerializer">
    <!-- ... -->
    <tag name="shopware.import_export.entity_serializer" priority="-400"/>
</service>

如果您的实体有一些需要特殊处理的字段,也许可以参考一下序列化程序。

vsikbqxv

vsikbqxv2#

您必须将实体添加到
vendor/shopware/administration/Resources/app/administration/src/module/sw-import-export/component/sw-import-export-edit-profile-general/index.js
到 * supportedEntities()* 方法
你可以这样做:

const { Component } = Shopware;

Component.override('sw-import-export-edit-profile-general', {
    methods: {
        supportedEntities() {
            const supportedEntities = this.$super('supportedEntities');
            supportedEntities.push({
                value: 'your_entity_name',
                label: "Entity Label",
                type: profileTypes.IMPORT_EXPORT,
            });
            
            return supportedEntities;
        }
    }
});

相关问题