After Extending Shopware 6 Product Entity, How Do I Save The Data When New Product Is Created?

huangapple go评论63阅读模式
英文:

After Extending Shopware 6 Product Entity, How Do I Save The Data When New Product Is Created?

问题

我根据Shopware文档扩展了Shopware 6的product表格,详情请参考添加复杂数据到现有实体

我已经添加了一个输入字段来接受用户数据,当产品保存时,如何将其保存到数据库中。我希望数据在product.written事件负载中可用。

After Extending Shopware 6 Product Entity, How Do I Save The Data When New Product Is Created?

英文:

I extended the Shopware 6 product table following Shopware documentation Adding complex data to existing entities.

I have added an input field to accept user data, how do I save it to the database when the product is saved. I want the data available in product.written event payload.

After Extending Shopware 6 Product Entity, How Do I Save The Data When New Product Is Created?

答案1

得分: 4

请看以下翻译:

由于 EntityExtension 仅允许添加关联或仅在运行时计算的字段(在运行时计算,无法持久化),我假设您已经注册了自定义实体并将其关联到 product

然后,您可以使用 product 实体的 extension 属性来存储与扩展实体的关联值。

我假设您已经重写了 sw-product-settings-form 以在管理界面中放置该字段:

Component.override('sw-product-settings-form', {
    template,
    inject: ['repositoryFactory'],
    computed: {
        sellerName: {
            get() {
                return this.product.extensions.yourEntity?.sellerName ?? null;
            },
            set(value) {
                if (!this.product.extensions.yourEntity) {
                    this.$set(this.product.extensions, 'yourEntity', this.repositoryFactory.create('your_entity').create());
                }

                this.$set(this.product.extensions.yourEntity, 'sellerName', value);
            },
        },
    }
});

在您的重写模板中:

<sw-text-field
    v-model="sellerName"
/>

然后,在保存产品时,对字段的更改将被持久化。这里有一个示例插件,以相同的方式扩展了客户模块。

英文:

Since EntityExtension only allows to add either associations or runtime only fields (evaluated during runtime and cannot be persisted), I assume you registered your own custom entity and associated it with product.

You can then use the extension property of the product entity to store values for associations to the extended entity.

I assume you did override sw-product-settings-form to place the field in the administration:

Component.override(&#39;sw-product-settings-form&#39;, {
    template,
    inject: [&#39;repositoryFactory&#39;],
    computed: {
        sellerName: {
            get() {
                return this.product.extensions.yourEntity?.sellerName ?? null;
            },
            set(value) {
                if (!this.customer.extensions.yourEntity) {
                    this.$set(this.product.extensions, &#39;yourEntity&#39;, this.repositoryFactory.create(&#39;your_entity&#39;).create());
                }

                this.$set(this.product.extensions.yourEntity, &#39;sellerName&#39;, value);
            },
        },
    }
});

In your override's template:

&lt;sw-text-field
    v-model=&quot;sellerName&quot;
/&gt;

Changes to the field will then be persisted when saving the product. Here's an example plugin that extends the customer module with an extension field in the same manner.

huangapple
  • 本文由 发表于 2023年4月1日 00:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定