<?php

namespace App\Models;

use CodeIgniter\Model;

/**
 * Base model for everything that lives in a tenant's own database (rooms,
 * bookings, folios, guests, staff users, activity log, ...).
 *
 * Extend this instead of CodeIgniter\Model and the connection is always the
 * current request's tenant database — no controller or model ever has to
 * pick a DB group or know a tenant is even involved. Requires that
 * TenantResolver has already run (it runs as a 'before' filter on every
 * tenant-scoped route) so Services::tenantDb() has something to return.
 *
 * IMPORTANT: never use the built-in `is_unique` validation rule here — it
 * has no idea a tenant connection exists and will query the CONTROL
 * database's `default` group instead. Use `tenant_unique[table.field,...]`
 * (same syntax, see App\Validation\CustomRules::tenant_unique) for any
 * uniqueness check on a tenant-scoped model.
 */
abstract class TenantModel extends Model
{
    public function __construct()
    {
        parent::__construct(service('tenantDb'));
    }
}
