英文:
Can not find association by name using Shopware 6 extension
问题
Shopware 6插件通过创建一个自定义实体并使用OneToMany关联它与现有实体。因此,我们应该能够为属性选项使用标签。
定义:
$query = <<<SQL
CREATE TABLE IF NOT EXISTS `wed_property_group_option_tag` (
`option_id` binary(16) NOT NULL,
`tag_id` binary(16) NOT NULL,
`created_at` datetime(3) NOT NULL,
`updated_at` datetime(3) DEFAULT NULL,
PRIMARY KEY (`option_id`, `tag_id`),
CONSTRAINT `fk.wed_property_group_option_tag.tag_id` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk.wed_property_group_option_tag.option_id` FOREIGN KEY (`option_id`) REFERENCES `property_group_option` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
namespace Wed\Core\System\Tag;
class TagDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'tag_id';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
(new FkField('option_id', 'optionId', PropertyGroupOptionDefinition::class))->addFlags(new Required()),
new ManyToOneAssociationField('option', 'option_id', PropertyGroupOptionDefinition::class, 'id'),
]);
}
}
<?php declare(strict_types=1);
namespace Wed\Core\Content\Property\Aggregate\PropertyGroupOption;
class PropertyGroupOptionDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'option_id';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
new OneToManyAssociationField('tags', FooDefinition::class, 'option_id')
]);
}
}
namespace Wed\Core\Content\Property\Aggregate\PropertyGroupOption;
class WedPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
public const ENTITY_NAME = 'wed_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),
]);
}
}
扩展:
namespace Wed\Extension\Content\Property\Aggregate\PropertyGroupOption;
class PropertyGroupOptionExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
new ManyToManyAssociationField('myTags', TagDefinition::class, WedPropertyGroupOptionTagDefinition::class, 'option_id', 'tag_id'),
);
}
public function getDefinitionClass(): string
{
return PropertyGroupOptionDefinition::class;
}
}
Administration JS:
Shopware.Component.override('sw-property-option-detail', {
inject: [
'repositoryFactory'
],
template,
data: function () {
return {
option: undefined
}
},
computed: {
repository() {
return this.repositoryFactory.create('wed_property_group_option_tag');
},
criteria() {
const criteria = new Criteria();
criteria.addAssociation('myTags');
return criteria;
},
},
created() {
this.repository = this.repositoryFactory.create('wed_property_group_option_tag');
this.repository.search(this.criteria, Shopware.Context.api)
.then(option => {
this.option = option;
});
}
});
选项详情页面未加载,并在控制台中出现以下错误:
"{"errors":[{"status":"500","code":"FRAMEWORK__ASSOCIATION_NOT_FOUND","title":"Internal Server Error","detail":"Can not find association by name myTags","meta":{"parameters":{"association":"myTags"}}}]}"
有人知道我做错了什么吗?
英文:
Shopware 6 plugin adds a custom entity and associate it via a OneToMany association with an existing entity. So we should be able to use tags for property options.
Definition:
$query = <<<SQL
CREATE TABLE IF NOT EXISTS `wed_property_group_option_tag` (
`option_id` binary(16) NOT NULL,
`tag_id` binary(16) NOT NULL,
`created_at` datetime(3) NOT NULL,
`updated_at` datetime(3) DEFAULT NULL,
PRIMARY KEY (`option_id`, `tag_id`),
CONSTRAINT `fk.wed_property_group_option_tag.tag_id` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk.wed_property_group_option_tag.option_id` FOREIGN KEY (`option_id`) REFERENCES `property_group_option` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
namespace Wed\Core\System\Tag;
class TagDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'tag_id';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
(new FkField('option_id', 'optionId', PropertyGroupOptionDefinition::class))->addFlags(new Required()),
new ManyToOneAssociationField('option', 'option_id', PropertyGroupOptionDefinition::class, 'id'),
]);
}
}
<?php declare(strict_types=1);
namespace Wed\Core\Content\Property\Aggregate\PropertyGroupOption;
class PropertyGroupOptionDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'option_id';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
new OneToManyAssociationField('tags', FooDefinition::class, 'option_id')
]);
}
}
namespace Wed\Core\Content\Property\Aggregate\PropertyGroupOption;
class WedPropertyGroupOptionTagDefinition extends MappingEntityDefinition
{
public const ENTITY_NAME = 'wed_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),
]);
}
}
Extension:
namespace Wed\Extension\Content\Property\Aggregate\PropertyGroupOption;
class PropertyGroupOptionExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
new ManyToManyAssociationField('myTags', TagDefinition::class, WedPropertyGroupOptionTagDefinition::class, 'option_id', 'tag_id'),
);
}
public function getDefinitionClass(): string
{
return PropertyGroupOptionDefinition::class;
}
}
Administration JS:
Shopware.Component.override('sw-property-option-detail', {
inject: [
'repositoryFactory'
],
template,
data: function () {
return {
option: undefined
}
},
computed: {
repository() {
return this.repositoryFactory.create('wed_property_group_option_tag');
},
criteria() {
const criteria = new Criteria();
criteria.addAssociation('myTags');
return criteria;
},
},
created() {
this.repository = this.repositoryFactory.create('wed_property_group_option_tag');
this.repository.search(this.criteria, Shopware.Context.api)
.then(option => {
this.option = option;
});
}
});
Option detail page doesn't load, I have these error in console:
"{\"errors\":[{\"status\":\"500\",\"code\":\"FRAMEWORK__ASSOCIATION_NOT_FOUND\",\"title\":\"Internal Server Error\",\"detail\":\"Can not find association by name myTags\",\"meta\":{\"parameters\":{\"association\":\"myTags\"}}}]}"
Do someone know, what I'm doing wrong?
答案1
得分: 2
你正在向 property_group_option
添加 myTags
关联,但你正在创建 wed_property_group_option_tag
的存储库。在 wed_property_group_option_tag
上没有 myTags
关联。
英文:
You're adding the myTags
association to property_group_option
but you're creating the repository for wed_property_group_option_tag
. There's no association myTags
on wed_property_group_option_tag
.
Shopware.Component.override('sw-property-option-detail', {
inject: [
'repositoryFactory'
],
template,
data() {
return {
options: []
}
},
computed: {
repository() {
return this.repositoryFactory.create('property_group_option');
},
criteria() {
const criteria = new Criteria();
criteria.addAssociation('myTags.tag');
return criteria;
},
},
created() {
this.repository.search(this.criteria, Shopware.Context.api)
.then(result => {
this.options = result;
this.options.forEach((option) => {
console.log(option.extensions.myTags);
});
});
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论