Motivation
The use case is to allow updating an entity with a HasOne relation, to delete the linked entity.
Currently when using ActiveModelEx you can use HasOneModel::set to set a new value for the field, but there is no way to just delete the value.
For example we might create a User with a Subscription, but we can't then easily delete the Subscription via the User, when it expires.
Proposed Solutions
Add a Delete variant to HasOneModel that deletes the linked entity, and something like HasOneModel::set_option(value: Option<E>) that acts like set when the value is Some, and deletes the entity when value is None.
I imagine there'd also be some additions to the builder to allow delete_{field_name} and set_option_{field_name}, e.g. delete_profile in the example in the repo below.
Additional Information
I asked about this on Discord and Huli recommended adding an issue.
I've created a minimal example on github. Note this also shows a bug that happens when using HasOneModel::set to replace an existing linked entity, this produces a unique constraint error. The suggested new Delete variant is shown as commented code on the entities from the repo.
Motivation
The use case is to allow updating an entity with a
HasOnerelation, to delete the linked entity.Currently when using
ActiveModelExyou can useHasOneModel::setto set a new value for the field, but there is no way to just delete the value.For example we might create a
Userwith aSubscription, but we can't then easily delete theSubscriptionvia theUser, when it expires.Proposed Solutions
Add a
Deletevariant toHasOneModelthat deletes the linked entity, and something likeHasOneModel::set_option(value: Option<E>)that acts likesetwhen the value isSome, and deletes the entity when value isNone.I imagine there'd also be some additions to the builder to allow
delete_{field_name}andset_option_{field_name}, e.g.delete_profilein the example in the repo below.Additional Information
I asked about this on Discord and Huli recommended adding an issue.
I've created a minimal example on github. Note this also shows a bug that happens when using
HasOneModel::setto replace an existing linked entity, this produces a unique constraint error. The suggested newDeletevariant is shown as commented code on the entities from the repo.