Kotchasan Framework Documentation
TableConfiguration
TableConfiguration
\Kotchasan\Database\TableConfiguration manages table name mappings and prefixes for database operations.
Creation
use Kotchasan\Database\TableConfiguration;
$config = new TableConfiguration([
'prefix' => 'app',
'tables' => [
'user' => 'users', // user -> app_users
'order' => 'customer_orders' // order -> app_customer_orders
]
]);Methods
getTableName()
Retrieve table name with prefix.
$config = new TableConfiguration(['prefix' => 'app']);
echo $config->getTableName('user'); // "app_user"
echo $config->getTableName('order'); // "app_order"With Table Mapping
$config = new TableConfiguration([
'prefix' => 'app',
'tables' => [
'user' => 'users', // logical -> physical
'order' => 'customer_orders'
]
]);
echo $config->getTableName('user'); // "app_users"
echo $config->getTableName('order'); // "app_customer_orders"
echo $config->getTableName('product'); // "app_product" (No mapping, uses original name)getPrefix()
Retrieve table prefix.
echo $config->getPrefix(); // "app"getTables()
Retrieve all table mappings.
$tables = $config->getTables();
// ['user' => 'users', 'order' => 'customer_orders']hasTable()
Check if table exists in configuration.
if ($config->hasTable('user')) {
echo "User table is configured";
}isValid()
Check if configuration is valid.
if (!$config->isValid()) {
echo "Configuration has errors";
}setLogger()
Set logger for error reporting.
$config->setLogger($logger);Configuration Validation
TableConfiguration automatically validates configuration:
- prefix must be a string.
- tables must be an array.
- table names must not contain null bytes or control characters.
// Invalid configuration - will use fallback configuration
$config = new TableConfiguration([
'prefix' => ['invalid'], // Must be string
'tables' => 'invalid' // Must be array
]);
echo $config->isValid(); // falseError Handling
If configuration is invalid:
- Log error (if logger is set).
- Use fallback configuration (empty prefix, no table mappings).
- Set
isValid()to false.
use Kotchasan\Logger\FileLogger;
$logger = new FileLogger('/path/to/logs/');
$config = new TableConfiguration($invalidConfig, $logger);
// Error will be logged to fileMethod Reference
| Method | Return | Description |
|---|---|---|
getTableName($table) |
string |
Table name with prefix |
getPrefix() |
string |
Table prefix |
getTables() |
array |
Table mappings |
hasTable($table) |
bool |
Check table in config |
isValid() |
bool |
Check config validity |
setLogger($logger) |
void |
Set logger |
Related Classes
- Database - Uses TableConfiguration internally
- SQL Functions - SQL helper functions