Shopware 6 OneToMany 关系

Shopware 6 OneToMany relation

我将在 Shopware 6 中定义一个 OneToMany 关系。在我的例子中,我将有一篇链接有许多标签的文章。 我知道这些情况在 Shopware 中可能过于复杂并且取决于很多因素。而且很难帮忙。 但如果给我介绍教程或代码示例,它会帮助我

使用AkArticle\Core\Content\Tag\tagCollection;

我的文章实体

class ArticleEntity extends Entity
{

    /**
     * @var TagCollection|null
     */
    protected $tag;
    protected $tagId;

public function getTagId(){ return $this->tagId; }
public function setTagId($value): void { $this->tagId = $value; }

public function getTag(): ?TagCollection
{
    return $this->tag;
}

public function setTag(TagCollection $tag): void
{
    $this->tag = $tag;
}
.
.
.
}

文章定义

<?php declare(strict_types=1);

namespace AkArticle\Core\Content\Article;

use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslatedField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Field\TranslationsAssociationField;
use AkArticle\Core\Content\Aggregate\ArticleTranslation\ArticleTranslationDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use AkTag\Core\Content\Tag\TagDefinition;

class ArticleDefinition extends EntityDefinition
{
    public const ENTITY_NAME = 'ak_artcle_plugin';
    public function getEntityName(): string
    {
        return self::ENTITY_NAME;
    }

    public function getEntityClass(): string
    {
        return ArticleEntity::class;
    }
    public function getCollectionClass(): string
    {
        return ArticleCollection::class;
    }

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
            (new StringField('name', 'name'))->addFlags(new Required()),
            (new BoolField('active','active'))->addFlags(new Required()),
            (new TranslatedField('title')),
            new IdField('tagId', 'tagId'),

          new TranslationsAssociationField(ArticleTranslationDefinition::class, 'ak_article_plugin_id'),
            new OneToManyAssociationField('tag', TagDefinition::class, 'id'),

        ]);
    }
}

文章-detals.html.twig

<sw-multi-select :label="$t('ak-article.detail.label.name-tag')" :options="tags"
                          v-model="article.tagId" >
 </sw-multi-select>

提交后出现此错误

{"0":{"code":"FRAMEWORK__WRITE_CONSTRAINT_VIOLATION","status":"400","detail":"This value should be of type string.","template":"This value should be of type string.","meta":{"parameters":[]},"source":{"pointer":"/0/tagId"}}}

我哪里错了。我试图找到一个解决方案,但找不到任何东西。我试图找到一些接近我的情况的代码示例,但我没有成功。

您需要的是文章和标签实体之间的多对多关联。 因此您需要创建一个 MappingDefinition,这在 docs.

中有详细说明

要在管理中显示该关联,您可以使用 sw-entity-many-to-many-select