easy-slug icon indicating copy to clipboard operation
easy-slug copied to clipboard

generateUniqueSlug not working for slugs without -% at the end

Open opheliadesign opened this issue 9 years ago • 33 comments

Clients began to complain about the wrong records being retrieved when they clicked on links and I assumed that I had forgotten to enable EasySlug when creating those entries. I checked my code and found that I had indeed used EasySlug::generateUniqueSlug().

If I have an entry with a slug of football-season on my seasons table, EasySlug will generate the next slug as football-season. However, if I change the slug on the table to football-season-1, EasySlug generates the next as football-season-2.

As a test I tried using the wrong table name to see the SQL query behind the scenes and noticed this:

$title = "Football Season";
$slug = EasySlug::generateUniqueSlug($title, "season", "slug");
select count(*) as aggregate from `season` where `slug` LIKE football-season-%

So it appears to be looking only for the number of items with -% in their slug, not simply a slug without the number of entries.

Am I missing something? This worked until recently, perhaps a breaking change somewhere?

opheliadesign avatar Jul 20 '15 14:07 opheliadesign

@opheliadesign is it creating same slug if count of slug is not present at end ?

viraj-khatavkar avatar Jul 20 '15 14:07 viraj-khatavkar

@viraj-khatavkar correct. So if football-season exists already, EasySlug still uses football-season for the next entry. It only increments if the first one is football-season-1

opheliadesign avatar Jul 20 '15 14:07 opheliadesign

okay will look at it and revert in some time

viraj-khatavkar avatar Jul 20 '15 14:07 viraj-khatavkar

Thanks!

opheliadesign avatar Jul 20 '15 14:07 opheliadesign

I am thinking that this is an issue in the queries within EasySlugRepository.

opheliadesign avatar Jul 20 '15 14:07 opheliadesign

got the issue, will resolve it in under an hour

viraj-khatavkar avatar Jul 20 '15 14:07 viraj-khatavkar

@opheliadesign Please do composer update once and check. I have resolved it. But, please check and let me know

viraj-khatavkar avatar Jul 20 '15 15:07 viraj-khatavkar

@viraj-khatavkar yes, that fixed it! Thanks so much!

opheliadesign avatar Jul 20 '15 15:07 opheliadesign

@opheliadesign Sure.. Anytime :+1: I will close this issue then ?

viraj-khatavkar avatar Jul 20 '15 15:07 viraj-khatavkar

@opheliadesign Please do composer update once again. There was one syntax issue that I resolved just now

viraj-khatavkar avatar Jul 20 '15 15:07 viraj-khatavkar

Yes, you may close this issue - and will do, thanks!

opheliadesign avatar Jul 20 '15 15:07 opheliadesign

Greetings! It appears that this is happening in v2.0.4 again.

opheliadesign avatar Jan 21 '16 02:01 opheliadesign

Any progress on this? Here is the code that I suspect is causing this:

if ( $count_of_matching_slugs > 0 )
        {
            $temporary_slug = $this->generateSlug( $string . " " . ( $count_of_matching_slugs + 1 ) , $separator );

            $flag = false;

            $i = 2;
            while($flag == false)
            {
                $exact_slugs = $this->_easy_slug_repo->getCountOfExactSlugs($table, $column, $temporary_slug);

                if($exact_slugs > 0)
                {
                    $temporary_slug = $this->generateSlug( $string . " " . ( $count_of_matching_slugs + $i ) , $separator );
                    $i++;
                }
                else
                {
                    $flag = true;
                }
            }
        }
        else {
            $temporary_slug = $temporary_slug . '-1'; // This will always start at -1 rather than just a plain slug
        }

opheliadesign avatar Jan 23 '16 22:01 opheliadesign

@opheliadesign can you please post an use case of the issue. I have been using this version in about 13 production applications and I haven't deduced this issue. It would be really helpful with the usecase

viraj-khatavkar avatar Jan 27 '16 06:01 viraj-khatavkar

@viraj-khatavkar the use case is generating a unique slug, as outlined on the initial issue report above. The first unique slug, according to the documentation and my preference, should not have a -1 suffix. This is being appended by:

....
} else {
            $temporary_slug = $temporary_slug . '-1'; // This will always start at -1 rather than just a plain slug
        }

To be clear, the code mentioned in this post and my last is from your package.

opheliadesign avatar Jan 27 '16 07:01 opheliadesign

@viraj-khatavkar any progress on this?

opheliadesign avatar Feb 08 '16 17:02 opheliadesign

@viraj-khatavkar starting a new project and still running into this, a post with the title "Test Post" and this code generates "test-post-1" instead of "test-post"

EasySlug::generateUniqueSlug($request->input('title'), 'posts');

opheliadesign avatar Mar 30 '16 18:03 opheliadesign

@opheliadesign any problem in that, I did this to avoid another conflicting issue in case of a string like "abc 1" and 2 times "abc"

viraj-khatavkar avatar Mar 30 '16 18:03 viraj-khatavkar

@viraj-khatavkar Most sites, including mine, seem to favor adding -x only if there are multiple posts with that title. Your documentation and previous implementation follow this -

"This function looks for similar slugs in the table/column name specified in parameters. If slugs with similar pattern are found it appends numeric digits at the end of slug as follows :"

your-string
your-string-2
your-string-3
your-string-4

If we could go back to this format, I'd really appreciate it. Thanks for replying so quickly :)

opheliadesign avatar Mar 30 '16 18:03 opheliadesign

Okay, give me an hour, I will look into that if I can go back to that :)

Apology for not replying for the last time you raised this, was bit busy in some work and then forgot :( Will look to this ASAP

viraj-khatavkar avatar Mar 30 '16 18:03 viraj-khatavkar

Sure, no problem! Really appreciate it. I've been busy since then, too ;)

opheliadesign avatar Mar 30 '16 18:03 opheliadesign

Just a thought, maybe a little RegEx magic to see if the title ends in a digit? That way you could prevent the issue that you mentioned but otherwise return the typical slug without an appended number if it's unique.

opheliadesign avatar Mar 30 '16 18:03 opheliadesign

Doing that is not an issue. If I check that last digit is a number or not I am stuck in following case :

  1. "abc" -> "abc"
  2. "abc" -> "abc-1"
  3. "abc-1" -> "abc-1" //Now, this is a conflicting case for me

viraj-khatavkar avatar Mar 30 '16 19:03 viraj-khatavkar

Hmm yes I see what you're saying. If there isn't already abc-1 in the database, start with abc-1. If it's the second abc-1, go to abc-1-2 and so on. Just a thought, have not looked at your source in a bit.

opheliadesign avatar Mar 30 '16 19:03 opheliadesign

What about an optional parameter in generateUnique that would allow us to pass in the name column. So it could look for existing entries like it does now, then compare them against the name. If the name has a digit at the end, you would know you still need to append a digit.

stygiansabyss avatar Apr 01 '16 18:04 stygiansabyss

@viraj-khatavkar nag nag nag nag ... sorry, just had to do it ;)

@stygiansabyss great idea, I like it.

opheliadesign avatar Apr 10 '16 20:04 opheliadesign

Ended up using https://github.com/cviebrock/eloquent-sluggable. For this package though, you could look at how he is doing it. He has you add a build_from and save_to on the model. Which was the idea I suggested. Knowing the name gives you more control over how to slug.

stygiansabyss avatar Apr 11 '16 13:04 stygiansabyss

@opheliadesign I found a solution, will implement it in a day, if you are still holding on to this package

viraj-khatavkar avatar Apr 16 '16 03:04 viraj-khatavkar

Honestly, I've started using the other package mentioned in this thread. However, I like the simplicity of yours. If you can implement a fix I will most likely use it again.

Thanks!

opheliadesign avatar Apr 16 '16 04:04 opheliadesign

hi i get it bugged me too, i try to solve this here if i have
title -------------- slug "abc" it will be "abc-2" "abc-1" it will be "abc-1-2"

raidenz avatar May 12 '16 10:05 raidenz