Kotchasan Framework Documentation

Kotchasan Framework Documentation

TableConfiguration

TH 05 Feb 2026 06:23

TableConfiguration

\Kotchasan\Database\TableConfiguration จัดการ table name mappings และ prefix สำหรับ database operations

การสร้าง

use Kotchasan\Database\TableConfiguration;

$config = new TableConfiguration([
    'prefix' => 'app',
    'tables' => [
        'user' => 'users',           // user -> app_users
        'order' => 'customer_orders' // order -> app_customer_orders
    ]
]);

Methods

getTableName()

ดึงชื่อตารางพร้อม prefix

$config = new TableConfiguration(['prefix' => 'app']);

echo $config->getTableName('user');    // "app_user"
echo $config->getTableName('order');   // "app_order"

กรณีมี 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" (ไม่มี mapping, ใช้ชื่อเดิม)

getPrefix()

ดึง table prefix

echo $config->getPrefix();  // "app"

getTables()

ดึง table mappings ทั้งหมด

$tables = $config->getTables();
// ['user' => 'users', 'order' => 'customer_orders']

hasTable()

ตรวจสอบว่ามี table ใน configuration หรือไม่

if ($config->hasTable('user')) {
    echo "User table is configured";
}

isValid()

ตรวจสอบว่า configuration valid หรือไม่

if (!$config->isValid()) {
    echo "Configuration has errors";
}

setLogger()

ตั้งค่า logger สำหรับ error reporting

$config->setLogger($logger);

Configuration Validation

TableConfiguration จะ validate configuration อัตโนมัติ:

  • prefix ต้องเป็น string
  • tables ต้องเป็น array
  • table names ต้องไม่มี null bytes หรือ control characters
// Invalid configuration - จะใช้ fallback configuration
$config = new TableConfiguration([
    'prefix' => ['invalid'],  // ต้องเป็น string
    'tables' => 'invalid'     // ต้องเป็น array
]);

echo $config->isValid();  // false

Error Handling

ถ้า configuration ไม่ valid จะ:

  1. Log error (ถ้ามี logger)
  2. ใช้ fallback configuration (prefix ว่าง, ไม่มี table mappings)
  3. Set isValid() เป็น false
use Kotchasan\Logger\FileLogger;

$logger = new FileLogger('/path/to/logs/');
$config = new TableConfiguration($invalidConfig, $logger);
// Error จะถูก log ไปยังไฟล์

Method Reference

Method Return Description
getTableName($table) string ชื่อตารางพร้อม prefix
getPrefix() string Table prefix
getTables() array Table mappings
hasTable($table) bool มี table ใน config หรือไม่
isValid() bool Config valid หรือไม่
setLogger($logger) void ตั้งค่า logger

คลาสที่เกี่ยวข้อง