[Vuejs]-Shopware ManyToMany with additional field

-1👍

You’re trying to create (insert) a new record when there already is an existing record with the same primary key(s). That’s why it expects you to update the existing record instead.

You need to try and fetch the existing record and either return the existing record or a new one.

// ...
inject: ['repositoryFactory'],

computed: {
    repository() {
        return this.repositoryFactory.create('sample_product_faq');
    },
},

methods: {
    async fetchOrCreate(id) {
        const existing = await this.repository.get(id, Shopware.Context.api);

        if (existing) {
            return existing;
        }

        return this.repository.create(Shopware.Context.api);
    },
},

You can also check beforehand whether a given entity is supposed to created or updated:

entity.isNew()

For the sake of completion I will say that you can force an update by setting entity._isNew = false but it is discouraged as it could lead to data loss, hence why the property is considered private.

Leave a comment