PHP Helpers and Utilities
Ragnos includes collection of global helper functions designed to speed up common tasks and improve application performance. Available throughout framework automatically.
Database and Cache Management
getCachedData()
Executes raw SQL query and stores result in CodeIgniter 4 cache system. Ideal for heavy queries or reports not needing real-time data.
function getCachedData(string $sql, array $params = [], ?string $cacheKey = null, int $ttl = 86400): array
Parameters
| Parameter | Type | Description |
|---|---|---|
$sql |
string |
SQL query to execute. |
$params |
array |
(Optional) Array of values for bound parameters (?) in query. |
$cacheKey |
string |
(Optional) Unique cache identifier. If null, hash generated automatically based on query and parameters. |
$ttl |
int |
(Optional) Time To Live in seconds. Default 86400 (24 hours). |
Return
Returns associative array with query results.
Usage Example
// In Model or Controller
public function getMetrics()
{
$sql = "SELECT
p.productLine,
SUM(od.quantityOrdered * od.priceEach) as Total
FROM products p
JOIN orderdetails od ON p.productCode = od.productCode
GROUP BY p.productLine";
// Cache result for 1 hour (3600 seconds) with specific key
return getCachedData($sql, [], 'dashboard_metrics', 3600);
}
⚠️ Warnings and Considerations
- Real Time Data: Do not use for critical data changing constantly (e.g. real time inventory with high concurrency, bank balances). Data served can be up to
$ttlseconds old. - Cache Invalidation: If using custom
$cacheKey(like'dashboard_metrics'), can manually clear that cache when data changes usingcache()->delete('dashboard_metrics'). Automatic keys harder to manually invalidate. - Dev Environment: Remember if developing and changing data, might see old data until cache cleared (
php spark cache:clear).
queryToAssocArray()
Executes SQL query and transforms result directly into associative array [id => value]. Extremely useful for populating dropdowns (<select>).
Example
// Get category list for Dropdown
$sql = "SELECT id, name FROM categories ORDER BY name ASC";
$options = queryToAssocArray($sql, 'id', 'name');
// Result: [1 => 'Electronics', 2 => 'Home', ...]
Debugging and Diagnostics
dbgConsola()
Sends PHP data directly to developer browser console. Uses console.log via script injection. Only works in development environment.
Example
dbgDie()
Kills script execution and returns provided data in JSON format. Perfect for debugging API/AJAX calls.
Format and UI
currency()
Formats numbers as currency respecting regional config (locale) without complex extensions.
Example
returnAsJSON()
Standardizes JSON responses for API or AJAX controllers. Automatically handles HTTP headers and UTF-8 encoding.