[英]Shopware 6 custom plugin, deleting extension on delete parent does not work
我按照手册Adding complex data to existing entities创建了一个扩展。
我的产品具有此扩展的另一个属性,例如product_color
。 删除产品时,prop(实体表) product_color
仍保留在数据库中。
// Migration%123%ProductColor.php
...
public function update(Connection $connection): void
{
$sql = <<<SQL
CREATE TABLE `product_color` (
`id` binary(16) NOT NULL,
`product_id` binary(16) DEFAULT NULL,
`color` tinyint(1) NOT NULL DEFAULT '0',
`created_at` datetime(3) NOT NULL,
`updated_at` datetime(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$connection->executeStatement($sql);
}
...
编辑#1
正如@Alex 所推荐的,我添加了标志,但扩展名保留在数据库中:
// custom/plugins/MyPlugin/src/Extension/ProductColor/ProductColorExtension.php
class ProductColorExtension extends EntityExtension
{
public function extendFields(FieldCollection $collection): void
{
$collection
->add(
(new OneToOneAssociationField(
'productColor',
'id',
'product_id',
ProductColorExtensionDefinition::class,
true
))->addFlags(new CascadeDelete())
);
}
public function getDefinitionClass(): string
{
return ProductDefinition::class;
}
}
问题:
有一个标志CascadeDelete
,您可以将其添加到您的关联字段中:
->addFlags(new CascadeDelete())
如果您使用dal:create:schema创建它,这应该导致 SQL 中的ON DELETE CASCADE
条目。
除了在实体定义中设置CascadeDelete
标志外,您还需要在数据库迁移中添加外键约束,并为ON DELETE
子句使用CASCADE
。
ALTER TABLE `product_color`
ADD CONSTRAINT `fk.product_color.product_id` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.