PGBI/cakephp3-soft-delete

Problem with belongsToMany queries

stefanozoffoli opened this issue · 3 comments

Esample:
PostsTable belongsToMany LabelsTable
In the database, each posts, labels, labels_posts has deleted field.

Database has these records:

posts (id, title, deleted):
(1, 'First article', null);

labels (id, title, deleted):
(1, 'First label', null),
(2, 'Second label', null);

labels_posts (id, post_id, label_id, deleted):
(1, 1, 1, '2015-05-05 11:48:27'),
(2, 1, 2, null);

When I select posts, both first and second labels are selected and associated to the article despite the deleted record in the bridge.

Here is the generated sql:

SELECT Posts.id AS `Posts__id`, Posts.title AS `Posts__title`, Posts.deleted AS `Posts__deleted`
FROM posts Posts
WHERE Posts.deleted IS NULL
ORDER BY Posts.id DESC
LIMIT 20
OFFSET 0

SELECT LabelsPosts.label_id AS `LabelsPosts__label_id`, LabelsPosts.id AS `LabelsPosts__id`, LabelsPosts.post_id AS `LabelsPosts__post_id`, LabelsPosts.deleted AS `LabelsPosts__deleted`, Labels.id AS `Labels__id`, Labels.title AS `Labels__title`, Labels.deleted AS `Labels__deleted`
FROM labels Labels
    INNER JOIN labels_posts LabelsPosts
    ON Labels.id = (LabelsPosts.label_id)
WHERE (LabelsPosts.post_id in (:c0) AND Labels.deleted IS NULL)

So Query does not add deleted condition to LabelsPosts.

Thank you!

PGBI commented

Indeed. Thanks for reporting! I've submitted a PR to cakephp to correct this issue. When cakephp 3.0.6 will be released this will be solved.

PGBI commented

It is now solved if you update cakephp to version 3.0.6.

Thank you! 😄