我有一个基本的
customAttributes
实体,有两个字段:attributeName
和 attributeValue
。目标是每当我在 Shopware 中检索产品时检索链接到 customAttributes
的 product
。
自定义属性定义.php
<?php
declare(strict_types=1);
namespace Meteor\OnboardingNielPlugin\Core\Content\CustomAttributes;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
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\ManyToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
#[AutoconfigureTag('shopware.entity.definition', ['entity' => 'custom_attributes'])]
class CustomAttributesDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'custom_attributes';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
public function getCollectionClass(): string
{
return CustomAttributesCollection::class;
}
public function getEntityClass(): string
{
return CustomAttributesEntity::class;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
new StringField('attribute_name', 'attributeName'),
new StringField('attribute_value', 'attributeValue'),
(new FkField('product_id', 'productId', ProductDefinition::class))->addFlags(new Required()),
new ManyToOneAssociationField('customAttributes2', 'product_id', ProductDefinition::class, 'id', false),
]);
}
}
如您所见,此定义与称为
customAttributes2
的产品相关联。预期的行为是通过添加关联来获取产品时检索 customAttributes
实体:
public function onProductExportProductCriteria(ProductExportProductCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('customAttributes2');
}
这不起作用。
我可以让它发挥作用的唯一方法是向核心中的
ProductDefinition
添加关联字段:
(new OneToManyAssociationField('customAttributes', CustomAttributesDefinition::class, 'product_id'))->addFlags(new CascadeDelete()),
做
public function onProductExportProductCriteria(ProductExportProductCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('customAttributes');
}
现在检索
customAttribute
。但编辑核心是不好的做法,所以我需要另一种方法。实际上,Shopware 应该有一种方法可以扩展核心中的实体字段,而无需实际编辑核心本身?否则无法设置 FK 或对自定义表/实体的引用。
为此,Shopware 希望您定义一个实体扩展。 请参阅:https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling/add-complex-data-to-existing-entities.html#creating-the-extension
基本上,您定义的服务与您的核心编辑完全相同:
在 services.xml 或类似文件中:
<services>
<service id="Meteor\OnboardingNielPlugin\Extension\Content\Product\CustomExtension">
<tag name="shopware.entity.extension"/>
</service>
</services>
在 PHP 中:
<?php declare(strict_types=1);
namespace Meteor\OnboardingNielPlugin\Extension\Content\Product;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField;
use Meteor\OnboardingNielPlugin\Core\Content\CustomAttributes\CustomAttributesDefinition;
class CustomExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection->add(
new OneToManyAssociationField('customAttributes', CustomAttributesDefinition::class, 'product_id'))
);
}
public function getDefinitionClass(): string
{
return ProductDefinition::class;
}
}
您可以将您的关联添加到标准中。