Search code examples
shopwareshopware6

Admin mask with custom entity with ManyToManyAssociation


I have an shopware6 admin extension where some custom entity is stored and loaded. This custom entity has a ManyToManyAssociation to another custom entity. In my mask I use a entity multi select component for the selection.

const category = this.categoryRepository.create();
            category.categoryId = this.newCategory;
            category.weight = this.newWeight;
            category.certificates = this.certificateCollection;
            this.categoryRepository.save(category).then(() => {
                this.isLoading = false;
                this.isSaveSuccessful = true;
                this.getList();
            }).catch((exception) => {
                this.isLoading = false;
                this.createNotificationError({
                    message: this.$tc(
                        'global.notification.notificationSaveErrorMessageRequiredFieldsInvalid'
                    )
                });
                throw exception;
            });
<sw-entity-multi-select
                    :label-property="labelPropertyCertificate"
                    :criteria="certificateCriteria"
                    entity-name="certificate"
                    :entity-collection="certificateCollection"
                    @change="changeCertificates"
            />

When sending the save request the content of my certificate selection is send with and js object with the property id. To create the correct entity I added some Entity creation process and build my own entity collection for the categoryCertificate collection.

Like this

const category = this.categoryRepository.create();
            category.categoryId = this.newCategory;
            category.weight = this.newWeight;
            category.certificates = new EntityCollection(
                this.categoryCertificateRepository.route,
                this.categoryCertificateRepository.entityName,
                Context.api,
            );
            this.certificates.forEach(function(item){
                const categoryCertificate = me.categoryCertificateRepository.create();
                categoryCertificate.categoryId = category.id;
                categoryCertificate.certificateId = item.id;
                category.certificates.add(categoryCertificate);
            });


            console.log(category);
            this.categoryRepository.save(category).then(() => {
                this.isLoading = false;
                this.isSaveSuccessful = true;
                this.getList();
            }).catch((exception) => {
                this.isLoading = false;
                this.createNotificationError({
                    message: this.$tc(
                        'global.notification.notificationSaveErrorMessageRequiredFieldsInvalid'
                    )
                });
                throw exception;
            });

The console.log shows this, which would be correct

categoryId: "77b959cf66de4c1590c7f9b7da3982f3" certificates: r(1) 0: Proxy [[Handler]]: Object [[Target]]: Object categoryId: "aa26daab83b04cc6af9a525e7ec1ce16" certificateId: "8c32fb76000844bcb4d850831fe2f6c1" extensions: {} _isNew: true [[Prototype]]: Object [[IsRevoked]]: false ...

The request payload shows this, which is not correct.

{id: "b96c923dcee941c7a05c0cb4f98de4d9", categoryId: "251448b91bc742de85643f5fccd89051", weight: 0,…} categoryId: "251448b91bc742de85643f5fccd89051" certificates: [{id: "10fb19bccdb0419ca5f4b7abe6561db2"}] id: "b96c923dcee941c7a05c0cb4f98de4d9" weight: 0

where are my categoryId & certificateId properties gone? Does the categoryRepository strip them out?

Edit solution

Thx to dneustadt

this.certificates.forEach(function(item){
                const categoryCertificate = me.careHintCategoryCertificateRepository.create();
                categoryCertificate.id = item.id;
                category.certificates.add(categoryCertificate);
            });

this works!


Solution

  • By using a ManyToManyAssociationField the values for the columns of the mapping table are resolved dynamically. Since these columns belong to the mapping definition, not to your actual CategoryCertificate entity, they get stripped. You don't need to provide these IDs when you use the respective ManyToManyAssociationField property to assign data from either side.