无法使用Shopware 6扩展按名称找到关联。

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

Can not find association by name using Shopware 6 extension

问题

Shopware 6插件通过创建一个自定义实体并使用OneToMany关联它与现有实体。因此,我们应该能够为属性选项使用标签。

定义:

        $query = <<<SQL
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;
SQL;
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'),
        ]);
    }
}
<?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;
    }
}

Administration 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"}}}]}"

有人知道我做错了什么吗?

英文:

Shopware 6 plugin adds a custom entity and associate it via a OneToMany association with an existing entity. So we should be able to use tags for property options.

Definition:

        $query = &lt;&lt;&lt;SQL
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;
SQL;
namespace Wed\Core\System\Tag;

class TagDefinition extends EntityDefinition
{
    public const ENTITY_NAME = &#39;tag_id&#39;;

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

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField(&#39;id&#39;, &#39;id&#39;))-&gt;addFlags(new PrimaryKey(), new Required()),
            (new FkField(&#39;option_id&#39;, &#39;optionId&#39;, PropertyGroupOptionDefinition::class))-&gt;addFlags(new Required()),
            new ManyToOneAssociationField(&#39;option&#39;, &#39;option_id&#39;, PropertyGroupOptionDefinition::class, &#39;id&#39;),
        ]);
    }
}
&lt;?php declare(strict_types=1);

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

class PropertyGroupOptionDefinition extends EntityDefinition
{
    public const ENTITY_NAME = &#39;option_id&#39;;

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

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField(&#39;id&#39;, &#39;id&#39;))-&gt;addFlags(new PrimaryKey(), new Required()),
            new OneToManyAssociationField(&#39;tags&#39;, FooDefinition::class, &#39;option_id&#39;)
        ]);
    }
}
namespace Wed\Core\Content\Property\Aggregate\PropertyGroupOption;

class WedPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
    public const ENTITY_NAME = &#39;wed_property_group_option_tag&#39;;

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

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

            new ManyToOneAssociationField(&#39;tag&#39;, &#39;tag_id&#39;, TagDefinition::class, &#39;id&#39;, false),
        ]);
    }
}

Extension:

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

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

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

Administration JS:

Shopware.Component.override(&#39;sw-property-option-detail&#39;, {
    inject: [
        &#39;repositoryFactory&#39;
    ],

    template,

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

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

            return criteria;
        },
    },

    created() {
        this.repository = this.repositoryFactory.create(&#39;wed_property_group_option_tag&#39;);

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

Option detail page doesn't load, I have these error in console:

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

Do someone know, what I'm doing wrong?

答案1

得分: 2

你正在向 property_group_option 添加 myTags 关联,但你正在创建 wed_property_group_option_tag 的存储库。在 wed_property_group_option_tag 上没有 myTags 关联。

英文:

You're adding the myTags association to property_group_option but you're creating the repository for wed_property_group_option_tag. There's no association myTags on wed_property_group_option_tag.

Shopware.Component.override(&#39;sw-property-option-detail&#39;, {
    inject: [
        &#39;repositoryFactory&#39;
    ],

    template,

    data() {
        return {
            options: []
        }
    },

    computed: {
        repository() {
            return this.repositoryFactory.create(&#39;property_group_option&#39;);
        },
        criteria() {
            const criteria = new Criteria();
            criteria.addAssociation(&#39;myTags.tag&#39;);

            return criteria;
        },
    },

    created() {
        this.repository.search(this.criteria, Shopware.Context.api)
            .then(result =&gt; {
                this.options = result;

                this.options.forEach((option) =&gt; {
                    console.log(option.extensions.myTags);
                });
            });
    }
});

huangapple
  • 本文由 发表于 2023年7月12日 21:56:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671371.html
匿名

发表评论

匿名网友

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

确定