简体   繁体   中英

Shopware 6 custom plugin, deleting extension on delete parent does not work

I have an extension created following the manual Adding complex data to existing entities .
My product has with this extension another property like eg product_color . On delete the product the prop (entity table) product_color still remains in the database.

// 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);
    }
...

EDIT #1
As recommend by @Alex, i've added flags, but the extension keep in the database :

// 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;
    }
}

Questions:

  • how to make the additional property deletable on delete its parent ( delete cascade )?
  • where are the corresponding manual how to achieve this?

There is a flag CascadeDelete which you can add to your association field:

->addFlags(new CascadeDelete())

This should lead to an ON DELETE CASCADE entry in the SQL, if you create it with dal:create:schema .

In addition to setting the CascadeDelete flag in the entity definition, you need to add a foreign key constraint with your database migration and use CASCADE for the ON DELETE subclause.

ALTER TABLE `product_color` 
ADD CONSTRAINT `fk.product_color.product_id` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM