PGBI/cakephp3-soft-delete

Query::delete() still hard delete

steefaan opened this issue · 2 comments

First of all, it's an awesome plugin you have written. Unfortunately I have a problem in combination with FriendsOfCake/crud and the bulk delete action. As you can see here the bulk delete action uses Query::delete() to build the delete query. This will generate a hard delete query which is not modified from the soft delete plugin. Probably this issue also occurs in other use cases than crud.

A possible solution would be to overwrite the delete() method inside ORM/Query.php which already exists in your plugin. @PGBI What do you think?

PGBI commented

Unfortunately, I can't override what's done by an other plugin. Even if I overwrite the delete() method inside \SoftDelete\ORM\Query, it won't help since in your example Crud won't be using \SoftDelete\ORM\Query::delete() anyway, it is calling directly \Cake\ORM\Query::delete().

@PGBI It should be possible to override it in \SoftDelete\ORM\Query because CRUD is in the correct context. Take a look to the output of debug($query); I placed before this line.

object(SoftDelete\ORM\Query) {

    '(help)' => 'This is a Query object, to get the results execute or iterate it.',
    'sql' => 'SELECT Users.id AS `Users__id`, Users.role_id AS `Users__role_id`, Users.slug AS `Users__slug`, Users.username AS `Users__username`, Users.email AS `Users__email`, Users.email_token AS `Users__email_token`, Users.first_name AS `Users__first_name`, Users.last_name AS `Users__last_name`, Users.password AS `Users__password`, Users.birthday AS `Users__birthday`, Users.gender AS `Users__gender`, Users.status AS `Users__status`, Users.last_login AS `Users__last_login`, Users.deleted AS `Users__deleted`, Users.modified AS `Users__modified`, Users.created AS `Users__created` FROM users Users WHERE (id IN (:c0) AND Users.deleted IS NULL)',
    'params' => [
        ':c0' => [
            'value' => '1a87eba0-2f98-4677-850d-fa6e321a9830',
            'type' => 'string',
            'placeholder' => 'c0'
        ]
    ],
    ...

As you can see, this query is already fine but here the query will be changed to a DELETE query. After this line, the query looks like the following:

object(SoftDelete\ORM\Query) {

    '(help)' => 'This is a Query object, to get the results execute or iterate it.',
    'sql' => 'DELETE FROM users WHERE id IN (:c0)',
    'params' => [
        ':c0' => [
            'value' => '4a6ed4ef-b713-41c8-a8b9-1a70f226e2a3',
            'type' => 'string',
            'placeholder' => 'c0'
        ]
    ],