Home > Enterprise >  How Do I Update A Database Column In Shopware Via The Administration
How Do I Update A Database Column In Shopware Via The Administration

Time:09-27

I have created an administration page in the Administration and I want to update information in the database when the page is loaded (in Vue js term CREATED). My code below does not do anything and I can not find any error. Help fix my code and how do I get errors from Shopware Administration.

const { Component, Mixin } = Shopware;
import template from './store-settings-page.html.twig'
Component.register('store-settings-page', {
   template,

   inject: [
    'repositoryFactory'
    ],

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

  data: function () {

    return {
        entity: undefined,
        entityId: '4e2891496c4e4587a3a7efe587fc8c80',
    }

   },

   computed: {

    storeKeysRepository() {
        return this.repositoryFactory.create('store_keys');
    },
    
   },


   created() {

    this.storeKeysRepository
            .get(this.entityId, Shopware.Context.api)
            .then(entity => {
                this.entity = entity;
            });

   /* const repository = this.storeKeysRepository();
    this.entity = repository.create(Shopware.Context.api);
    this.entity.name = 'Diekedie';
    repository.save(this.entity, Shopware.Context.api);
    */

    // a function which is called over the ui

        this.entity.name = 'updated';
        // sends the request immediately
        this.storeKeysRepository
            .save(this.entity, Shopware.Context.api)
            .then(() => {
                // the entity is stateless, the data has be fetched from the server, if required
                this.storeKeysRepository
                    .get(this.entityId, Shopware.Context.api)
                    .then(entity => {
                        this.entity = entity;
                    });
            });

        },
    
  
});

CodePudding user response:

Looks like you're not awaiting the fetch request meaning your entity would still be undefined when it reaches the save call on the repository.

You should move the save call inside the chained method of the first request. Also unless you have some fields which are indexed or computed server side, you might not need to refetch the entity after the successful save call.

this.storeKeysRepository.get(this.entityId, Shopware.Context.api).then((entity) => {
    this.entity = entity;
    this.entity.name = 'updated';
    this.storeKeysRepository.save(this.entity, Shopware.Context.api);
});
  • Related