Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twinnable HasMany relation with cascade delete doesn't work as expected #250

Open
shaoshiva opened this issue May 25, 2016 · 0 comments
Open

Comments

@shaoshiva
Copy link
Collaborator

shaoshiva commented May 25, 2016

For a Twinnable HasMany relation you can configure the cascade_delete by setting true or false.

With cascade_delete set to false there are 2 possible behaviours :

  • if key_to is not a common field on the target model, the item will be soft deleted (key_to set to 0), the items in the others contexts won't be deleted.
  • if key_to is a common field on the target model, the item and the items in the others contexts will be soft deleted (the key_to is set to 0) .

But with cascade_delete set to true there is only one behaviour :

  • regardless whether the key_to is a common field or not, the item will be hard deleted (removed from database), and items in the others contexts won't be deleted.

... instead it should be :

  • if key_to is not a common field on the target model, the item will be hard deleted (removed from database), the items in the others contexts won't be deleted.
  • if key_to is a common field on the target model, the item and the items in the others contexts will be hard deleted (removed from database).

To fix this issue we will have to write a custom save method on Nos\Orm_Twinnable_HasMany instead of using the save method of the parent class Orm\HasMany.

Here is the part of the save method that hard deletes the related item :

$model_from->unfreeze();
$obj->delete();
$model_from->freeze();

It could be patched this way :

$model_from->unfreeze();

// Deletes the items in the other contexts if key_to is a common field
if (check_if_key_to_is_a_common_field()) { // @todo
    foreach ($obj->find_other_contexts()  as $other_obj) {
        $other_obj->delete();
    }
}

// Deletes the item in the current context
$obj->delete();

$model_from->freeze();

Will try to start a pull request when I'll have time.

In the meantime if you need to remove the items in all the contexts, use the soft delete (cascade_delete set to false).

Edit: updated patch code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant