CakeDC/utils

potential bug when using both SoftDeleteBehavior and SluggableBehavior

angelxmoreno opened this issue · 2 comments

originally posted in #123

I have a Model called Job. The minimum fields for my explanation are:

id char(36) primary
name        varchar(100) non unique
slug        varchar(150) unique
is_deleted  boolean
deleted     datetime

As you can see, the slug field is unique and the name is not. Given an entry like name: Job Example would create a slug job-example. If I were to softdelete this entry then attempt to add another entry by the same name, the SluggableBehavior would not find the first entry with the job-example slug and would attempt to use that already existing slug. The same thing would happen if say, I had a slug best-job1 but best-job2 was soft deleted. The SluggableBehavior would attempt to use best-job2 because the find would not return the existing row with the best-job2 slug.

I was surprised to have found no mention of this since these are to very popular behaviors. I am curious to know what solutions I could put in place to be able to use both behaviors and still get unique slug entries.

We usually write decoupled code that has if possible no dependencies on other modules. Sluggable does not need and should not need (lookup SoC, separation of concerns) to know about Softdeleteable and the other way around.

I guess the issue is the priority here. Change the priority of the behaviors to make sure that Sluggable is called before Softdeleteable and the problem should be gone. See http://book.cakephp.org/2.0/en/core-libraries/collections.html#object-callback-priorities

Please let me know if this resolved your issue.

Closing this now, assuming it's working based on my instructions.