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

[BUG]: SoftDelete behavior tries to create new record of belongsTo relationship #16453

Open
bazsa001 opened this issue Oct 14, 2023 · 0 comments
Labels
bug A bug report status: unverified Unverified

Comments

@bazsa001
Copy link

bazsa001 commented Oct 14, 2023

Describe the bug

When trying to delete a model record with SoftDelete behavior enabled on the model, Phalcon attempts to re-create another model record that the deleted record belongsTo.

To Reproduce

  1. Define two models (Foo and Bar);
  2. Define a property on Foo and validate it using Validator\Uniqueness;
  3. Define a one-to-many relationship between Foo and Bar:
  • in Foo::initialize(): $this->hasMany('id', 'Bar', 'foo_id', ["alias" => "Bars"]);
  • in Bar::initialize(): $this->belongsTo('foo_id', 'Foo', 'id');
  1. Define the built-in SoftDelete behavior for Bar using the field Bar::deleted;
  2. Create one record of Foo and save it, then create one record of Bar with foo_id refering to the recently created Foo record and save it;
  3. Try to delete the Bar record using its delete() method.

During the deletion process, the uniqueness validator of Foo fails, as Phalcon is trying to create a new record of a model using the data of the already existing record. It's not evident to me why Phalcon wants to touch any related records at all during soft delete, but here's the output of debug_print_backtrace() as called from Foo::validation():

#0 [internal function]: Foo->validation()
#1 [internal function]: Phalcon\Mvc\Model->fireEventCancel('validation')
#2 [internal function]: Phalcon\Mvc\Model->preSave(Object(Phalcon\Mvc\Model\MetaData\Memory), false, 'id')
#3 [internal function]: Phalcon\Mvc\Model->save()
#4 [internal function]: Phalcon\Mvc\Model->preSaveRelatedRecords(Object(Phalcon\Db\Adapter\Pdo\Mysql), Array)
#5 [internal function]: Phalcon\Mvc\Model->save()
#6 [internal function]: Phalcon\Mvc\Model\Behavior\SoftDelete->notify('beforeDelete', Object(Bar))
#7 [internal function]: Phalcon\Mvc\Model\Manager->notifyEvent('beforeDelete', Object(Bar))
#8 [internal function]: Phalcon\Mvc\Model->fireEventCancel('beforeDelete')
#9 /srv/app/controllers/BarController.php(806): Phalcon\Mvc\Model->delete()
#10 [internal function]: BarController->deleteAction()
#11 [internal function]: Phalcon\Dispatcher\AbstractDispatcher->callActionMethod(Object(BarController), 'deleteAction', Array)
#12 [internal function]: Phalcon\Dispatcher\AbstractDispatcher->dispatch() #13 /srv/public/index.php(33): Phalcon\Mvc\Application->handle('/bar/delet...')

Calling the save() method of the Bar instance directly does not produce this issue.

Expected behavior
I would not expect related records to be re-saved during a soft delete, but even if they do get re-saved, it should be an update operation, not a create operation.

Details

  • Phalcon version: 5.3.1
  • PHP Version: 8.1
  • Operating System: Ubuntu
  • Installation type: Compiled from source
  • Server: Nginx
  • Other related info (Database, table schema): MySQL
@bazsa001 bazsa001 added bug A bug report status: unverified Unverified labels Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug report status: unverified Unverified
Projects
None yet
Development

No branches or pull requests

1 participant