Helper_Backend
Utility trait for backend operations in a PHP application
The Helper_Backend
trait provides utility functions for backend operations, including database management, CSV handling, and regex testing.
Database Functions
createSqlDump($dbName)
Creates a SQL dump of an external database and imports it into a local database.
The name of the database to be dumped and imported.
This function performs the following steps:
- Connects to an external database
- Retrieves all tables from the external database
- Creates a local database if it doesn’t exist
- For each table:
- Generates CREATE TABLE syntax
- Exports data in chunks
- Handles BLOB data separately
- Imports data into the local database
- Adjusts character encoding for ‘name’ columns
This function uses hardcoded database credentials. In a production environment, these should be stored securely and not in the source code.
executeSqlChunk(sqlChunk)
Executes a chunk of SQL commands on the local database.
PDO connection to the local database.
A string containing multiple SQL commands separated by semicolons.
This function:
- Temporarily disables foreign key checks
- Executes each SQL command in the chunk
- Catches and reports any PDOExceptions
- Re-enables foreign key checks after execution
Regex Functions
regex_test()
Tests the regex_match
function with a specific regex pattern.
The result of matching ‘is_unique[metatag.name]’ against the ‘is_unique’ pattern.
CSV Upload Functions
csv_upload($itemId)
Displays the CSV upload form.
The ID of the item associated with the CSV upload.
upload_file()
Handles the file upload process for CSV files.
JSON response with:
- success: Boolean indicating if the upload was successful
- path: The server path where the file was saved
- filename: The new filename of the uploaded file
csvUpdateWhereId()
Processes an uploaded CSV file and updates database records based on its contents.
JSON response with:
- status: ‘success’ if the operation was successful
- returndata: An array of updated data
This function:
- Reads the CSV file
- Maps CSV columns to database fields
- Updates database records based on a specified ID column
- Deletes the CSV file after processing
The function uses hardcoded values for CSV structure and database mapping. These should be configurable for flexibility in different use cases.
Configuration Variables
The csvUpdateWhereId()
function uses several configuration variables that should be customized based on the CSV structure and database schema:
The delimiter used in the CSV file.
The key in the CSV file that corresponds to the item ID in the database.
The name of the database table to be updated.
An array of keys in the CSV file, in the order they appear. These keys should correspond to database column names.