英文:
Attach Shopware 6 tags to Property values
问题
有人能否就如何将Shopware 6的标签附加到属性(仅一个值)上提供建议?
是否有一种方法可以重用某些组件?我认为标签是使用数据关联完成的。
--
我已经设置好了,但现在我遇到了这个错误(在单击后端管理选项列表中的属性选项详细信息模态框时无法打开):
app.js?16886789168584353:2 在当前模块中捕获到了一个错误:ReferenceError: option未定义
当我删除选择器时,它可以正常打开:
<sw-entity-tag-select
v-if="option"
v-model="option.extensions.myTags"
:label="$tc('global.sw-tag-field.title')"
/>
以下是js代码:
Shopware.Component.override('sw-property-option-detail', {
inject: [
'repositoryFactory'
],
template,
data: function () {
return {
option: undefined
}
},
computed: {
repository() {
return this.repositoryFactory.create('a7filter_property_group_option_tag');
},
criteria() {
const criteria = new Criteria();
criteria.addAssociation('myTags');
return criteria;
},
},
created() {
this.repository = this.repositoryFactory.create('a7filter_property_group_option_tag');
this.repository.search(this.criteria, Shopware.Context.api)
.then(option => {
this.option = option;
});
}
});
也许有人可以提供建议?
英文:
Can someone advice on making it possible to attach Shopware 6 tags to Property (only one) values?
Is there a way to reuse some components? I assume tags are done using data associations.
--
I have set it up, but now I get this error (while not being able to open Property option detail modal on click in a backend administration options list):
app.js?16886789168584353:2 An error was captured in current module: ReferenceError: option is not defined
It opens well, when I remove the selector:
<sw-entity-tag-select
v-if="option"
v-model="option.extensions.myTags"
:label="$tc('global.sw-tag-field.title')"
/>
Here is the js:
Shopware.Component.override('sw-property-option-detail', {
inject: [
'repositoryFactory'
],
template,
data: function () {
return {
option: undefined
}
},
computed: {
repository() {
return this.repositoryFactory.create('a7filter_property_group_option_tag');
},
criteria() {
const criteria = new Criteria();
criteria.addAssociation('myTags');
return criteria;
},
},
created() {
this.repository = this.repositoryFactory.create('a7filter_property_group_option_tag');
this.repository.search(this.criteria, Shopware.Context.api)
.then(option => {
this.option = option;
});
}
});
Maybe someone can give an advice?
答案1
得分: 3
Create a custom entity for the mapping
class MyPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
final public const ENTITY_NAME = 'my_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),
]);
}
}
Create an EntityExtension
class PropertyGroupOptionExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
new ManyToManyAssociationField('myTags', TagDefinition::class, MyPropertyGroupOptionTagDefinition::class, 'option_id', 'tag_id'),
);
}
public function getDefinitionClass(): string
{
return PropertyGroupOptionDefinition::class;
}
}
Add association to criteria for property_group_option
where they are fetched
criteria.addAssociation('myTags');
In the template of the component you can use sw-entity-tag-select
for assigning tags
<sw-entity-tag-select
v-if="option"
v-model="option.extensions.myTags"
:label="$tc('global.sw-tag-field.title')"
/>
英文:
Create a custom entity for the mapping
class MyPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
final public const ENTITY_NAME = 'my_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),
]);
}
}
Create an EntityExtension
class PropertyGroupOptionExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
new ManyToManyAssociationField('myTags', TagDefinition::class, MyPropertyGroupOptionTagDefinition::class, 'option_id', 'tag_id'),
);
}
public function getDefinitionClass(): string
{
return PropertyGroupOptionDefinition::class;
}
}
Add association to criteria for property_group_option
where they are fetched
criteria.addAssociation('myTags');
In the template of the component you can use sw-entity-tag-select
for assigning tags
<sw-entity-tag-select
v-if="option"
v-model="option.extensions.myTags"
:label="$tc('global.sw-tag-field.title')"
/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论