vue.js 使用管理将多对多数据插入Shopware 6数据库

inn6fuwd  于 2022-12-19  发布在  Vue.js
关注(0)|答案(1)|浏览(143)

我已经在管理中创建了一个插件,我想将带有车辆的manyToMany产品插入到Shopware 6数据库中。从下面的代码中,我尝试将'92961afbc50e4380b3af86b257630ade'插入到'vehicles_product'表的'product_id'列中:

import template from './sw-vehicles-import.html.twig';

const { Component, Mixin } = Shopware;

Component.register('sw-vehicles-import', {
    template,
    inject: ['importExport', 'repositoryFactory', 'feature'],

    mixins: [
        Mixin.getByName('notification'),
    ],

    metaInfo() {
        return {
            title: this.$createTitle()
        };
    },

    data() {
        return {
            importFile: null,
            repository: null,
            entity: undefined,
        };
    },
    
    computed: {
    },
     
    created() {
        this.repository = this.repositoryFactory.create('vehicles');
    },

    methods: {
        onStartProcess() {

            this.entity = this.repository.create(Shopware.Context.api);

            this.entity.categoryFilter = 'CategoryName';
            this.entity.featureFilter = 'FeatureName';
            this.entity.products.productId = '92961afbc50e4380b3af86b257630ade';

            this.repository.save(this.entity, Shopware.Context.api);
        }
   }
});

构建过程不起作用,我做错了什么?你能帮帮我吗?

cwdobuhd

cwdobuhd1#

如果关联尚不存在,则需要为该关联创建一个新的实体集合。

const { EntityCollection } = Shopware.Data;

if (!this.entity.products) {
    this.entity.products = new EntityCollection(
        '/product',
        'product',
        Shopware.Context.api
    );
}

const product = await this.repositoryFactory.create('product').get('92961afbc50e4380b3af86b257630ade', Shopware.Context.api);

this.entity.products.add(product);

this.repository.save(this.entity, Shopware.Context.api);

相关问题