vue.js Shopware 6 Admin API:如何使用默认实体属性存储新实体

disho6za  于 2023-03-24  发布在  Vue.js
关注(0)|答案(2)|浏览(109)

如何defaultCurrency.id在sw-entity-single-select***before***this.repository.save()中为新实体属性(如www.example.com)设置默认值?
是否可能?或者只能在save()之后,并在emitted event change()上侦听新请求?
重写createNewEntity()会将属性值存储在我的数据库中...

...
createNewEntity() {
    const entity = this.repository.create(this.context);
    entity.name = this.newEntityName;
    entity.currencyId = this.defaultCurrency.id;
    
   ... 
}

...但debug.utils.js响应错误(罕见,因为创建了实体):
[Only Entities with "name" as the only required field are creatable.]

r7s23pms

r7s23pms1#

createNewEntity() {
    const entity = this.repository.create(this.context);
    entity.name = this.newEntityName;
    entity.currencyId = this.defaultCurrency.id;
    
   ... 
}

从理论上讲,这应该已经工作了,因为你也复制了原始方法的其余部分。你检查过开发人员工具的网络选项卡来查看对POST /api/{entity}的附带请求的响应吗?该请求应该失败,并在响应中给出更详细的错误消息。可能有更多的必填字段,而不仅仅是currencyId
总的来说,你已经有了正确的想法来覆盖组件,不幸的是,在不复制整个createNewEntity方法的情况下这样做有点棘手。
我快速地创建了一个覆盖,它修饰了存储库的create方法,以便在创建后立即修改实体。

Component.override('sw-entity-single-select', {
    props: {
        additionalCreationProperties: {
            type: Object,
            required: false,
            default() {
                return {};
            },
        },
    },

    computed: {
        repository() {
            const repository = this.$super('repository');
            const that = this;
            const decorator = {
                create(context, id) {
                    const entity = super.create(context, id);

                    Object.entries(that.additionalCreationProperties).forEach(([key, value]) => {
                        that.$set(entity, key, value);
                    });

                    return entity;
                },
            };

            Object.setPrototypeOf(decorator, repository);

            return decorator;
        },
    }
});

这引入了一个新的属性additionalCreationProperties,然后可以将其与sw-entity-single-select一起使用,为创建的实体提供默认值。

<sw-entity-single-select
    entity="entity"
    allow-entity-creation
    :additional-creation-properties="{ currencyId: defaultCurrency.id }"
/>
bf1o4zei

bf1o4zei2#

首先,我extend()sw-entity-single-select而不是override()。据我所知,这种配置避免了意外覆盖sw-entity-single-select在子组件/s。如果我错了,请告诉我。

...
Component.extend('custom-entity-single-select','sw-entity-single-select', {
    ...
}

然后
您是否检查了开发者工具的网络选项卡以查看对POST /API/{entity}的附带请求的响应?该请求应该失败,并在响应中提供更详细的错误消息。
没有关于的网络通知
在调试找出错误的来源后(没有深入寻找原因),createNewEntity()中的this.$emit('option-select', ...);在扩展组件中被禁用:

...
Component.extend('custom-entity-single-select','sw-entity-single-select', {
    ...
    createNewEntity() {
        const entity = this.repository.create(this.context);
        entity.name = this.newEntityName;
        entity.currencyId = this.defaultCurrency.id;

        this.repository.save(entity, this.context).then(() => {
            this.lastSelection = entity;
            this.$emit('change', entity.id, entity);
                
            // this.$emit('option-select', Utils.string.camelCase(this.entity), entity);
            this.createNotificationSuccess({
                message: this.$tc(
                    'global.sw-single-select.labelEntityAddedSuccess',
                    0,
                    {
                        term: entity.name,
                        entity: this.entityCreationLabel,
                    },
                ),
            });
        }).catch(() => {
            this.createNotificationError({
                message: this.$tc('global.notification.notificationSaveErrorMessage', 0, { entityName: this.entity }),
                });
                Shopware.Utils.debug.error('Only Entities with "name" as the only required field are creatable.');
                this.isLoading = false;
            });        
    }

}

相关问题