Shopware6 产品扩展

Shopware6 Product extension

提问人:Moss 提问时间:11/3/2023 最后编辑:Moss 更新时间:11/4/2023 访问量:36

问:

我正在开发一个仓库管理插件,我需要将仓库分配给产品。 一个产品只有一个仓库,但一个仓库有多个产品 我有一个仓库实体和一个同时包含product_id和warehouse_id的交汇点表

我添加了实体扩展,例如

 $collection->add(
            (new ManyToManyAssociationField(
                'warehouse',
                WarehouseDefinition::class,
                ProductWarehouseDefinition::class,
                'product_id',
                'warehouse_id'
            ))->addFlags(new Inherited())
        );

在产品详细信息中,我需要树枝,它不起作用<sw-entity-single-select>

编辑

产品扩展.php

class ProductExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new ManyToManyAssociationField(
                'warehouse',
                WarehouseDefinition::class,
                ProductWarehouseDefinition::class,
                'product_id',
                'warehouse_id'
            ))->addFlags(new Inherited())
        );
    }
}

产品仓库集合.php。

    protected function defineFields(): FieldCollection{ return new FieldCollection([
        (new FkField('product_id', 'product_id', ProductDefinition::class, 'id'))->addFlags(new PrimaryKey(),new Required()),
        (new FkField('warehouse_id', 'warehouse_id', WarehouseDefinition::class, 'id'))->addFlags(new PrimaryKey(),new Required()),
        (new ReferenceVersionField( ProductDefinition::class))->addFlags(new PrimaryKey(), new Required()),
        (new ManyToOneAssociationField('warehouse', 'warehouse_id', WarehouseDefinition::class)),
        (new ManyToOneAssociationField('product', 'product_id', ProductDefinition::class)),
        (new DateTimeField('created_at', 'created_at')),
        (new DateTimeField('updated_at', 'updated_at')),
    ]);

sw-product-detail-base.html.twig

{% block sw_product_detail_base_settings_card %}
{% parent %}

<sw-inherit-wrapper v-if="!isLoading"
            v-model="product.extensions.warehouse"
            :hasParent="!!parentProduct.id"
            label="Warehouse"
            isAssociation
            @inheritance-remove="saveProduct"
            @inheritance-restore="saveProduct">
            <template #content="{ currentValue, isInherited, updateCurrentValue }">
                    <sw-entity-single-select 
                    :key="isInherited" 
                    :value="currentValue"
                    :disabled="isInherited" 
                    entity="warehouse"
                    placeholder="select warehouse" 
                    allow-entity-creation
                    show-clearable-button @change="updateCurrentValue" />
            </template>
    </sw-inherit-wrapper>
   </sw-card>
{% endblock %}
插件实体 店件6

评论

0赞 Ezycod 11/3/2023
首先,我建议改用 OneToOneAssociation,请提供有关 sw-entity-single-select 字段的管理扩展的完整代码
0赞 Moss 11/3/2023
产品id在ProductWarehouse表中为FK,如何在这里使用onetoOne获取仓库和产品
0赞 Ezycod 11/3/2023
我建议遵循该案例的文档(developer.shopware.com/docs/guides/plugins/plugins/framework/...不需要 ManyToMany,因为每个产品只能分配一个仓库。但是,如果没有提供代码,我们仍然无能为力!但是,无论如何,它都应该与您的 ManyToManyAssociation 一起使用
0赞 Moss 11/3/2023
有什么方法可以将所选对象作为 v-model 发送?
1赞 Moss 11/7/2023
@Ezycod我使用了 OneToOneAssociation 并在 javascript 中做了一些更改,它现在可以工作了,感谢您的回复

答: 暂无答案