Assume that I have 3 tables namely sales_order , sales_order_items , invoice and set ON UPDATE = NO ACTION ON DELETE = NO ACTION.
Now I need to have the ability do delete the sales order. But if there is a relation exists in any other table it means that that sales order is used somewhere else and i need to prevent deletion.
Ex: I have sales_order_id = 34 , I want to check its exists in any other tables.
Previously to achieve the same thing I used transactions, something like below
$db = new database();
//start transaction
$db->start_trans();
//try to delete the sales order with ID = 34
$db->exec( 'DELETE FROM SALES_ORDER WHERE ID = 34' );
//check transaction success or failure
if( $db->trans_status() == true ){
//THERE IS NO RELATION EXISTS
//ROLLBACK
$db->rollback_trans();
#Soft Delete the record
$db->exec( 'UPDATE sales_order SET is_deleted = 1 WHERE id = 34' );
}else{
//RELATION EXISTS FOR ID = 34 IN SOME OTHER TABLES
}
the above code works, but the problem is with sales_order_items. Because it is a child table of sales_order and if it have contents then the transaction will fail and will try to execute the else part.
But in fact sales_order_items is a property of sales_order and I need to DELETE that particular sales_order (don't care about items).
I am expecting something like this
$relations = $db->get_relation( 'sales_order.id', '34' );
Expected Output
array( 'sales_order_items','invoice','another_table' .... );
Note: The above is an example only, I have number of tables and it's not possible to go through each table and check ID exists.
on delete cascadeand then you're set emulating its behavior anyway through your application?deletion.UPDATE sales_order SET is_deleted = 1 WHERE id = 34.