Are your MySQL tables using MyISAM? No support for foreign key constraints with cascading deletes means you must track entity relationships in business logic. When related parts of an application assuming many-to-many connected records exist, permanent SQL DELETE commands may only be called when it’s confirmed the cardinality is zero.

So add an `active` column to the table which will soft delete records. All queries to the database should check this `active` column is true when retrieving records (or at the very least, indicate that record is no longer used.)

For my example, store and warehouse locations had a hierarchical structure so it required recursively checking parent-child relationships before a location could be deleted from the database. I’m not going to get into the limitations of defining such a tree in the same database table as that’s a tutorial for another day! The DataMapper has_many property can be used to check relationships. In your case, the has_one iteration may also have to be looped through to confirm the object is free for deletion.

DataMapper ORM model

The is_related() method can be used to choose between permanent deletion or soft deactivation by calling the delete() action in the below CodeIgniter controller.