Thread: CRUD Class

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Rep Power

    CRUD Class

    Hello everyone.
    This is my first post here.
    I'm implementing a very simple PHP class (learning purposes only) for doing CRUD tasks and I would love to receive some tips/advices about good practices.
    As every CRUD class, this one has a delete() method which is used only to delete an entry from the database (MySQL in my case).
    My question is:

    For the delete() method, is it good to check if the entries that match the WHERE clause really exist before effectively deleting them?
    In other words: before executing the deletion itself, should I include as part of the code for the delete() method the instructions for checking if the entries really exist or this checking should be done *outside* elsewhere (maybe in an isolated method)? Or yet, do you guys think that shouldn't be a concern, since the DBMS doesn't complain (i.e. doesn't emit any errors) when deleting entries that don't exist in the given table?

    What is the best option? Which approach the PHP frameworks use for handling this situation?
    Thanks a lot for any help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Rep Power
    Hello chapeupreto,

    Do you want to inform the user that the record to be deleted doesn't exist? If so, check.

    Also, consider not deleting at all, but flagging the record as being deleted. To do so, add a column in your record table such as date_deleted (DATETIME) or deleted (tinyint), and when the user deletes the record, don't really delete the record from the table but update the record to either set date_deleted to NOW() or set deleted to 1.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Pennsylvania, USA
    Rep Power
    Don't bother checking. If you delete something which doesn't exist you get a successful query which affected zero rows.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo