Joshua Behrens
Joshua Behrens

Reputation: 818

How to include all associations when creating and merging a version in Shopware DAL?

When I edit the order in the admin, a version is created. I get a version clone commit for each order table. Now I found an entity with an association to an order that is not part of the clone. I noticed that the relation from that entity to the order is version aware but the entity itself is not version aware. Do I need to make it version aware as well so the order clone and later merge works? Because right now the merge is broken due to FK issues and I assume it misses a clone in the first place.

TL:DR; what are important parts in entity definitions to check when one want to support order editing in admin (which triggers version create and merge)?

Here is the entity definition in question that somehow is not part of version cloning of orders:

(new IdField('id', 'id'))->addFlags(new ApiAware(), new PrimaryKey(), new Required()),

(new FkField('order_id', 'orderId', OrderDefinition::class))->addFlags(new ApiAware(), new Required()),
(new ReferenceVersionField(OrderDefinition::class, 'order_version_id'))->addFlags(new ApiAware(), new Required()),

// ... entity content
(new CustomFields())->addFlags(new ApiAware()),


(new ManyToOneAssociationField('order', 'order_id', OrderDefinition::class, 'id', false))->addFlags(new ApiAware()),

Upvotes: 1

Views: 457

Answers (1)

dneustadt
dneustadt

Reputation: 13161

Updated possible cause

Did you also extend the OrderDefinition to add the association on that side?

(new OneToManyAssociationField('customEntities', CustomEntityDefinition::class, 'order_id'))->addFlags(new ApiAware(), new CascadeDelete())

That would explain why it's ignored in the clone process.

Solution update: The OneToManyAssociationField must have the CascadeDelete flag or else it won't be used as an association to be cloned.

Old answer (regarding m2m relations, not the case here)

Yes, you need to make the associations version aware. Otherwise you'd run into conflicts with foreign key constraints.

Let's say in case of a regular many-to-many relation you'd have the columns order_id and my_entity_id for the mapping. The combination of those two would form a unique constraint.

In your case you'd need to make the MappingEntityDefinition version aware and add a ReferenceVersionField for the order relation. So to allow version aware associations you'd instead need the columns order_id, order_version_id and my_entity_id. These would then form the unique constraint and allow to create versions with the associations intact.

Upvotes: 1

Related Questions