英文:
After Extending Shopware 6 Product Entity, How Do I Save The Data When New Product Is Created?
问题
我根据Shopware文档扩展了Shopware 6的product
表格,详情请参考添加复杂数据到现有实体。
我已经添加了一个输入字段来接受用户数据,当产品保存时,如何将其保存到数据库中。我希望数据在product.written
事件负载中可用。
英文:
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.
答案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('sw-product-settings-form', {
template,
inject: ['repositoryFactory'],
computed: {
sellerName: {
get() {
return this.product.extensions.yourEntity?.sellerName ?? null;
},
set(value) {
if (!this.customer.extensions.yourEntity) {
this.$set(this.product.extensions, 'yourEntity', this.repositoryFactory.create('your_entity').create());
}
this.$set(this.product.extensions.yourEntity, 'sellerName', value);
},
},
}
});
In your override's template:
<sw-text-field
v-model="sellerName"
/>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论