gpt4 book ai didi

Shopware 6 OneToMany 关系

转载 作者:行者123 更新时间:2023-12-05 02:49:13 28 4
gpt4 key购买 nike

我将在 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>

enter image description here

提交后我得到这个错误

{"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 .

关于Shopware 6 OneToMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64080503/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com