将产品扩展连接到自定义实体并添加到管理界面。

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

Connect product extension to custom entity and add to administration

问题

I’ve created a custom entity, lets call it my_entity, and added a StringField called name. After that I created an admin section with a list view and detail page. The list view shows all the entries from my_entity and within the detail page you can create or edit an entry. So far so good.

然后,我创建了一个自定义实体,让我们称之为my_entity,并添加了一个名为name的StringField。之后,我创建了一个具有列表视图和详细页面的管理部分。列表视图显示my_entity中的所有条目,在详细页面中,您可以创建或编辑条目。到目前为止,一切都还好。

Then I created a product extension. But this is the part where I’m stuck. The extension will use a separate DB table, my_entity_extension. The extension and the DB table need to reference to a product and my_entity. A product can have multiple references to an entry from my_entity.

然后我创建了一个产品扩展。但这是我卡住的地方。该扩展将使用一个单独的数据库表,名为my_entity_extension。扩展和数据库表需要引用一个产品和my_entity。一个产品可以有多个对my_entity条目的引用。

How will the productExtension, in particular the extendFields, look like? Do I need to use a OneToOneAssociationField or a ManytoOne?

产品扩展,特别是extendFields,应该是什么样的?我需要使用OneToOneAssociationField还是ManyToOne?

Then my next question, I couldn’t quit find this in the documentation. I extended the product view tab in the administration. Now I want to add a sw-entity-multi-select component which is linked to my_identity but I need to save the selection in the product extension. How can I get this to work in the Twig and JS file?

接下来的问题是,我在管理中扩展了产品视图选项卡。现在我想添加一个与my_identity关联的sw-entity-multi-select组件,但我需要将选择保存在产品扩展中。我该如何在Twig和JS文件中使其工作?

Also what I noticed is that if I enable the autoload in the product extension, then the product overview list is empty. No products are showing in the admin. Once I set it to false, it’s showing again.

另外我注意到的是,如果在产品扩展中启用autoload,那么产品概览列表将为空。管理界面中不显示任何产品。一旦将其设置为false,它又会显示出来。

A lof of questions but hopefully someone can help me out.

有很多问题,但希望有人可以帮助我解决。

英文:

I’ve created a custom entity, lets call it my_entity, and added a StringField called name. After that I created an admin section with a list view and detail page. The list view shows all the entries from my_entity and within the detail page you can create or edit an entry. So far so good.

Then I created a product extension. But this is the part where I’m stuck. The extension will use a separate DB table, my_entity_extension. The extension and the DB table need to reference to a product and my_entity. A product can have multiple references to an entry from my_entity.

How will the productExtension, in particular the extendFields, look like? Do I need to use a OneToOneAssociationField or a ManytoOne?

Then my next question, I couldn’t quit find this in the documentation. I extended the product view tab in the administration. Now I want to add a sw-entity-multi-select component which is linked to my_identity but I need to save the selection in the product extension. How can I get this to work in the Twig and JS file?

Also what I noticed is that if I enable the autoload in the product extension, then the product overview list is empty. No products are showing in the admin. Once I set it to false, it’s showing again.

A lof of questions but hopefully someone can help me out.

答案1

得分: -1

"Do I need to use a OneToOneAssociationField or a ManytoOne?

This really depends on your intended use. Shall multiple products be associated to one single record of your custom entity? Then it's ManyToOne. Shall there be one unique record of your custom entity per plugin? Then it's OneToOne. See the documentation with examples to each kind of association.

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

Assuming you override sw-product-settings-form to place the field in the administration:

Component.override('sw-product-settings-form', {
    template,
    inject: ['repositoryFactory'],
    computed: {
        someProperty: {
            get() {
                return this.product.extensions.yourEntity?.someProperty ?? 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, 'someProperty', value);
            },
        },
    }
});

In your override's template:

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

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.

英文:

> Do I need to use a OneToOneAssociationField or a ManytoOne?

This really depends on your intended use. Shall multiple products be associated to one single record of your custom entity? Then it's ManyToOne. Shall there be one unique record of your custom entity per plugin? Then it's OneToOne. See the documentation with examples to each kind of association.

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

Assuming you 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: {
        someProperty: {
            get() {
                return this.product.extensions.yourEntity?.someProperty ?? 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;someProperty&#39;, value);
            },
        },
    }
});

In your override's template:

&lt;sw-text-field
    v-model=&quot;someProperty&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月20日 03:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058119.html
匿名

发表评论

匿名网友

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

确定