Hey there,
I´m currently working with inheritance in Shopware and unfortunately I cannot understand why the following simple example does not work as expected.
Does the association inheritance only work for the ProductDefinition or is the inheritance also possible for own definitions? Unfortunately I can’t get this to work, in my short simplified example below the inheritance column ‘leader’ always remains NULL:
DepartmentDefinition
class DepartmentDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'department';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
public function isInheritanceAware(): bool
{
return true;
}
public function getEntityClass(): string
{
return DepartmentEntity::class;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new ApiAware(), new PrimaryKey(), new Required()),
(new ParentFkField(self::class))->addFlags(new ApiAware()),
(new ParentAssociationField(self::class, 'id'))->addFlags(new ApiAware()),
(new ChildrenAssociationField(self::class))->addFlags(new ApiAware()),
(new FkField('leader_id', 'leaderId', DepartmentLeaderDefinition::class))->addFlags(new ApiAware(), new Inherited()),
(new ManyToOneAssociationField('leader', 'leader_id', DepartmentLeaderDefinition::class, 'id'))->addFlags(new ApiAware(), new Inherited())
]);
}
}
DepartmentLeaderDefinition
class DepartmentLeaderDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'department_leader';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new ApiAware(), new PrimaryKey(), new Required()),
]);
}
}
Migration:
public function update(Connection $connection): void
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS department_leader (
id BINARY(16) NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS department (
id BINARY(16) NOT NULL,
parent_id BINARY(16) NULL,
leader_id BINARY(16) NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NULL,
PRIMARY KEY (id),
CONSTRAINT FK_department_leader_id FOREIGN KEY (leader_id)
REFERENCES department_leader (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FK_department_parent_id FOREIGN KEY (parent_id)
REFERENCES department (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$connection->executeStatement($sql);
$this->updateInheritance($connection, 'department', 'leader');
}
Expected behaviour when updating leaderId:
The leader column (Inheritance column) should also receive the leaderId (ManyToOneAssociationField)
If, for example, I change the manufacturerId of the ProductDefinition via the Sync API, the manufacturer UUID is also updated in the manufacturer column.
Behaviour that has occurred:
If I update the leaderId of the DepartmentDefinition via the Sync API, the leader column (Inheritance column) remains NULL.
Unfortunately, I cannot understand at all why this simple example does not work.
Maybe one of you can help me!
Thanks a lot!
Frank