You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6扩展报错:无法找到myTags关联,求错误排查

Shopware 6插件关联错误排查:无法找到myTags关联

我在Shopware 6插件中添加了自定义实体,计划通过关联让属性选项使用标签,但当前属性选项详情页加载失败,控制台抛出FRAMEWORK__ASSOCIATION_NOT_FOUND错误,提示找不到myTags关联。以下是我的代码实现:

数据库表定义

CREATE TABLE IF NOT EXISTS `wed_property_group_option_tag` (
    `option_id` binary(16) NOT NULL,
    `tag_id` binary(16) NOT NULL,
    `created_at` datetime(3) NOT NULL,
    `updated_at` datetime(3) DEFAULT NULL,
    PRIMARY KEY (`option_id`, `tag_id`), 
    CONSTRAINT `fk.wed_property_group_option_tag.tag_id` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `fk.wed_property_group_option_tag.option_id` FOREIGN KEY (`option_id`) REFERENCES `property_group_option` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Tag实体定义

namespace Wed\Core\System\Tag;

class TagDefinition extends EntityDefinition
{
    public const ENTITY_NAME = 'tag_id';

    public function getEntityName(): string
    {
        return self::ENTITY_NAME;
    }

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
            (new FkField('option_id', 'optionId', PropertyGroupOptionDefinition::class))->addFlags(new Required()),
            new ManyToOneAssociationField('option', 'option_id', PropertyGroupOptionDefinition::class, 'id'),
        ]);
    }
}

PropertyGroupOption实体定义

<?php declare(strict_types=1);

namespace Wed\Core\Content\Property\Aggregate\PropertyGroupOption;

class PropertyGroupOptionDefinition extends EntityDefinition
{
    public const ENTITY_NAME = 'option_id';

    public function getEntityName(): string
    {
        return self::ENTITY_NAME;
    }

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
            new OneToManyAssociationField('tags', FooDefinition::class, 'option_id')
        ]);
    }
}

关联映射实体定义

namespace Wed\Core\Content\Property\Aggregate\PropertyGroupOption;

class WedPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
    public const ENTITY_NAME = 'wed_property_group_option_tag';

    public function getEntityName(): string
    {
        return self::ENTITY_NAME;
    }

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new FkField('option_id', 'optionId', PropertyGroupOptionDefinition::class))->addFlags(new PrimaryKey(), new Required()),
            (new FkField('tag_id', 'tagId', TagDefinition::class))->addFlags(new PrimaryKey(), new Required()),
            
            new ManyToOneAssociationField('option', 'option_id', PropertyGroupOptionDefinition::class, 'id', false),

            new ManyToOneAssociationField('tag', 'tag_id', TagDefinition::class, 'id', false),
        ]);
    }
}

实体扩展代码

namespace Wed\Extension\Content\Property\Aggregate\PropertyGroupOption;

class PropertyGroupOptionExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            new ManyToManyAssociationField('myTags', TagDefinition::class, WedPropertyGroupOptionTagDefinition::class, 'option_id', 'tag_id'),
        );
    }

    public function getDefinitionClass(): string
    {
        return PropertyGroupOptionDefinition::class;
    }
}

后台管理端JS代码

Shopware.Component.override('sw-property-option-detail', {
    inject: [
        'repositoryFactory'
    ],

    template,

    data: function () {
        return {
            option: undefined
        }
    },

    computed: {
        repository() {
            return this.repositoryFactory.create('wed_property_group_option_tag');
        },
        criteria() {
            const criteria = new Criteria();
            criteria.addAssociation('myTags');

            return criteria;
        },
    },

    created() {
        this.repository = this.repositoryFactory.create('wed_property_group_option_tag');

        this.repository.search(this.criteria, Shopware.Context.api)
            .then(option => {
                this.option = option;
            });
    }
});

错误信息

{"errors":[{"status":"500","code":"FRAMEWORK__ASSOCIATION_NOT_FOUND","title":"Internal Server Error","detail":"Can not find association by name myTags","meta":{"parameters":{"association":"myTags"}}}]}"

请问我哪里出错了?


问题修正要点

  • 实体名称错误:
    • TagDefinition的ENTITY_NAME应设为tag(对应Shopware原生标签表),而非tag_id;PropertyGroupOptionDefinition的ENTITY_NAME应设为property_group_option,你当前的定义覆盖了原生实体,导致关联匹配失败。
  • 关联目标错误:
    • PropertyGroupOptionDefinition中的OneToManyAssociationField引用了不存在的FooDefinition,需替换为WedPropertyGroupOptionTagDefinition::class。
  • JS查询对象错误:
    • myTags是property_group_option实体的关联字段,JS中应创建property_group_option仓库进行查询,而非wed_property_group_option_tag仓库。
  • 实体扩展逻辑错误:
    • 若要扩展原生PropertyGroupOption实体,不应重新定义PropertyGroupOptionDefinition,而是通过EntityExtension扩展字段即可,避免覆盖原生实体结构。

内容的提问来源于stack exchange,提问作者Stacker1427

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 10:27:49