商店软件6:为产品创建继承的实体扩展



我想在Shopware 6.3.5.4中创建一个产品扩展,并遵循了本指南。它应该是一个OneToOne协会,我只想为父产品填充此扩展,因为其中的数据对于产品的所有变体都是相同的。因此,如果加载了变体,它应该加载父级的扩展。

这是我的分机:

class MyExtension extends EntityExtension
{
public const EXTENSION_NAME = 'myExtension';
public function extendFields(FieldCollection $collection): void
{
$collection->add(
(new OneToOneAssociationField(
self::EXTENSION_NAME,
'id',
'product_id',
MyExtensionDefinition::class,
true
))->addFlags(new Inherited())
);
}
public function getDefinitionClass(): string
{
return ProductDefinition::class;
}
}

这是我的扩展定义的defineFields函数:

protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
new VersionField(),
(new StringField('material_1', 'material1')),
(new StringField('material_2', 'material2')),
(new StringField('material_3', 'material3')),
(new StringField('material_4', 'material4')),
(new FkField('product_id', 'productId', ProductDefinition::class))->addFlags(new Required()),
(new ReferenceVersionField(ProductDefinition::class))->addFlags(new Required()),
new OneToOneAssociationField('product', 'product_id', 'id', ProductDefinition::class, false),
]);
}

这是我的扩展实体:

class MyExtensionEntity extends Entity
{
use EntityIdTrait;
protected string $productId;
protected string $productVersionId;
protected ?ProductEntity $product = null;
protected ?string $material1 = null;
protected ?string $material2 = null;
protected ?string $material3 = null;
protected ?string $material4 = null;
// getters and setters
}

最后是我的迁移:

class Migration1622484776MyExtension extends MigrationStep
{
use InheritanceUpdaterTrait;
public function getCreationTimestamp(): int
{
return 1622484776;
}
public function update(Connection $connection): void
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `my_extension` (
`id`                        BINARY(16)      NOT NULL,
`version_id`                BINARY(16)      NOT NULL,
`material_1`                VARCHAR(255)    NULL,
`material_2`                VARCHAR(255)    NULL,
`material_3`                VARCHAR(255)    NULL,
`material_4`                VARCHAR(255)    NULL,
`product_id`                BINARY(16)      NOT NULL,
`product_version_id`        BINARY(16)      NOT NULL,
`created_at`                DATETIME(3)     NOT NULL,
`updated_at`                DATETIME(3)     NULL,
PRIMARY KEY (`id`, `version_id`),
CONSTRAINT fk_my_extension__product 
FOREIGN KEY (`product_id`,`product_version_id`) REFERENCES `product` (`id`,`version_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$connection->executeUpdate($sql);
$this->updateInheritance($connection, 'product', MyExtension::EXTENSION_NAME);
}
public function updateDestructive(Connection $connection): void
{
// implement update destructive
}
}

在我运行导入产品和扩展的导入脚本后,它在数据库中看起来很好。只有父产品的扩展,并且在CCD_;myExtension";继承。它填充了所有变体的父产品ID,所以一切看起来都很好。但如果我通过DAL加载变体,则扩展总是"0";空";。它不是从父产品中提取的。我的代码中缺少什么来实现这一点?

我发现了我的错误。在迁移过程中,我通过调用$this->updateInheritance($connection, 'product', MyExtension::EXTENSION_NAME)为product表创建了一个新列。但在我的EntityExtension中,我使用了id作为OneToOneAssociation的存储名称(第二个参数(。我必须使用新列作为存储名称,如下所示:

public function extendFields(FieldCollection $collection): void
{
$collection->add(
(new OneToOneAssociationField(
self::EXTENSION_NAME,
self::EXTENSION_NAME,
'product_id',
MyExtensionDefinition::class,
true
))->addFlags(new Inherited())
);
}

现在,继承正在发挥作用。

最新更新