Fetching records with slug instead of ID

后端 未结 2 1498
抹茶落季
抹茶落季 2021-01-02 20:23

I\'m currently trying to find the best way (in term of usability and performance) when dealing with a situation like fetching records tagged with a specific tag, or category

相关标签:
2条回答
  • 2021-01-02 20:57

    The first one is better, but can the slugs possibly be changed? In that case you'd need to have a redirect table (e.g. "some-article-about-dogs" is now "article-about-dogs-and-cats").

    0 讨论(0)
  • 2021-01-02 21:03

    With the first URL style and your current db design, you can do this:

    select ...
    from   posts p
    join   posts_to_tags pt on pt.post_id = p.post_id
    join   tags t on t.id = pt.tag_id
    where  t.slug = [url slug value];
    

    As long as tags.slug is indexed, this should be very efficient, hardly any different from

    select ...
    from   posts p
    join   posts_to_tags pt on pt.post_id = p.post_id
    where  pt.tag_id = [url tag ID];
    
    0 讨论(0)
提交回复
热议问题