Suppose i have this db table schema:
id | article | slug
1 hey guys how are you? hey-guys-how-are-you?
I’m trying keeping my db route field unique
route is always the message field camelized like this this-is-a-title-but-now-is-a-route
so inserting the same route produce a db error of duplicated key
so which is a good practice to control and insert always unique routes?
thanks
Because you’ve tagged CodeIgniter, I would suggest pre-checking the value of the slug field, then increment the value if need be. The CI string helper has a
increment_stringfunction that will take care of that for you.increment_string()Increments a string by appending a number to it or increasing the number. Useful for creating “copies” or a file or duplicating database content which has unique titles or slugs.
Usage example:
So
this-is-a-routebecomesthis-is-a-route-1and
this-is-a-route-1becomesthis-is-a-route-2etc.