doupa8922 2013-05-09 08:09 采纳率: 100%
浏览 141

MySQL触发器检查slug是否已经存在,如果确实存在,则更改slug

I'm a long time php/mysql developer however have only recently started looking into using triggers. From my understanding, you can use them to perform actions when a record is added/modified/deleted.

With this in mind, I had a thought about a current simple cms script I'm putting together. As it stands I've been planning on having it so that a URL slug is created based on the page name (e.g a page titled 'Hello World' would have the slug 'hello-world').

Generally when I've done this before, I've checked to see if the slug is already in use, and thrown back an error to the user asking them to modify it, or in somecases where the slug isnt too important, appended '-1' to the end of it, or '-2', '-3', etc if the number was already in use.

However with triggers, its given me a possible idea, however I have a feeling it may be 'out of scope' for what triggers can/should be used for.

My thinking was that as a record is inserted, it runs a query to check and see if the slug is already in use, then if it is, changes it. The only problem I cant work out how to solve (if it is indeed even possible) is to keep iterating through records until it finds a free one (e.g so if 'hello-world', 'hello-world-1' and 'hello-world-2' were taken, it'd essentially loop through all three, then finally arrive at 'hello-world-3' as the slug that's free to use.

Is this worth pursuing or am I overcomplicating things here for the sake of trying to use a trigger?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用yolov5-7.0目标检测报错
    • ¥15 对于这个问题的解释说明
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥20 java在应用程序里获取不到扬声器设备