MySQL stored procedures or php code?

前端 未结 11 768
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 14:35

A general question, without a specific case in mind - is it usually preferred to use MySQL stored procedures over writing a PHP script that performs the same calculations an

11条回答
  •  旧时难觅i
    2020-12-29 15:15

    I would say "don't make too much magic with the database". The worst case would be for a new developper on the project to notice that ** an operation ** is done, but he cannot see where in the code it is. So he keeps looking for it. But it's done in the database.

    So if you do some "invisible" database operations (I'm thinking about triggers), just write it in some code documentation.

    // add a new user
    $user = new User("john", "doe");
    $user->save();
    // The id is computed by the database see MYPROC_ID_COMPUTATION
    print $user->getId();
    

    In the other hand, writing functions for the DB is a good idea, and would provide the developer a good abstraction layer.

    // Computes an ID for the given user
    DB->execute("SELECT COMPUTE_ID(" . $user->getLogin() . ") FROM DUAL");
    

    Of course this is all pseudo-code, but I hope you understand my obscure idea.

提交回复
热议问题