Shopware 6 OneToMany relation

Viewed 1181

I am going to define a OneToMany relation in the Shopware 6 .in my case i am going to have an article linked with many tags. I understand that those situations can be too complex in the Shopware and depends on many things. and it is hard to help. But if introduce to me a tutorial or code example, it will help me

use AkArticle\Core\Content\Tag\tagCollection;

My ArticleEntity

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;
}
.
.
.
}

ArticleDefinition

<?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>

enter image description here

after submit i get this error

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

What is my wrong .I tried to found a solution but can't found anything .I tried to found to some codes example close to my situation but i don't succeed.

1 Answers

What you need is a many to many association between the article and tag entity. Therefore you need to create a MappingDefinition, this is explained in detail in the docs.

To display that association in the administration you can use the sw-entity-many-to-many-select.

Related