Kotchasan Framework Documentation

Kotchasan Framework Documentation

TableConfiguration

EN 05 Feb 2026 06:23

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();  // false

Error Handling

If configuration is invalid:

  1. Log error (if logger is set).
  2. Use fallback configuration (empty prefix, no table mappings).
  3. Set isValid() to false.
use Kotchasan\Logger\FileLogger;

$logger = new FileLogger('/path/to/logs/');
$config = new TableConfiguration($invalidConfig, $logger);
// Error will be logged to file

Method 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