This topic is locked
[SOLVED]

Cascade delete with files in details/child record

8/1/2025 8:21:23 PM
PHPRunner General questions
H
headingwest author

Hi All,

I want cascade delete records which I normally do using a foreign key in MySQL. In this case the child record has a file stored on the server.

Any ideas how to delete the file along with the child record when the master record gets deleted?

Thanks for your help.

ChatGPT 8/1/2025

Certainly! In PHPRunner, to achieve a cascade delete that also removes associated files from the server when a master record is deleted, you'll generally need to handle this through custom code. Here’s a step-by-step guide on how to implement this:

Steps to Implement Cascade Delete with File Deletion:

  1. Identify the Relationship: Ensure you know the foreign key relationships between your master and child tables. You should also know where the file paths are stored in the child record.


  2. Create Before Delete Event: In PHPRunner, go to the Events section of your master table and find the Before Delete event. This is where you will write custom code to handle the deletion.


  3. Write Custom Code: In the Before Delete event for the master table, you will need to:

    • Retrieve the child records associated with the master record.
    • Delete the files associated with those child records from the server.
    • Delete the child records from the database.



Here’s a sample code snippet to help you get started:

// Get ID of the master record being deleted
$masterId = $values['id']; // Adjust according to your key field

// Query to get child records based on master ID
$sql = "SELECT * FROM child_table WHERE master_id = $masterId"; // Adjust table and field names
$rs = db_query($sql);

// Loop through child records
while ($row = db_fetch_array($rs)) {
// Assuming 'file_path' is the field storing the file path
$filePath = $row['file_path'];

// Check if the file exists and delete it
if (file_exists($filePath)) {
unlink($filePath); // Delete the file
}
}

// Now delete the child records
$sqlDeleteChild = "DELETE FROM child_table WHERE master_id = $masterId";
db_exec($sqlDeleteChild);
  1. Test Your Implementation: After adding the code, test your application to ensure that when a master record is deleted, the associated child records and files are also deleted without issues.

Important Considerations:

  • Backup Files: Before implementing file deletions, ensure you have backups in case you accidentally delete important files.
  • Permissions: Ensure that your web server has the necessary permissions to delete files on the server.
  • Error Handling: Consider adding error handling to your code to manage cases where file deletion might fail or where child records might not be found.
  • Database Transactions: If you want to ensure that both deletions (files and records) are successful, consider using database transactions.

By following these steps, you should be able to implement a cascade delete in PHPRunner that removes both records and their associated files.

H
headingwest author 8/4/2025

If anyone needs to do this, you need to decode the json for the file name. Replace the loop with:

// Loop through child records
while ($row = db_fetch_array($rs)) {
// Assuming 'filename' is the field storing the file path
$jsonStr = $row['filename'];

// Decode JSON
$data = json_decode($jsonStr, true);

$filename = $data[0]['name'];

// Check if the file exists and delete it
if (file_exists($filename)) {
unlink($filename); // Delete the file
}
}