【问题标题】:Shopware 6 OneToMany relationShopware 6 OneToMany 关系
【发布时间】:2021-01-12 18:09:33
【问题描述】:

我将在 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'),

        ]);
    }
}

article-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"}}}

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

【问题讨论】:

  • 请显示您的 EntityDefinition 的完整代码,否则我无法提供帮助。
  • 嗨 Sebastian.i 已经更新了问题。谢谢你
  • 您真的想要标签和文章之间的一对多关系吗?意思是一篇文章只能有一个标签,或者你需要ManyToMany,意思是文章可以有多个标签,标签可以分配给多篇文章。使用 oneToMany,您将需要在管理中使用 sw-single-select 而不是多选。
  • 嗨。我想要一篇与标签相关的文章。(一篇文章可以有多个标签)。所以我应该使用多对多关系。谢谢

标签: shopware shopware6


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 2021-08-09
    • 2017-01-23
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多