Database

Configuration

The framework supports both MySQL and SQLite databases:

MySQL Configuration

DB_TYPE=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

SQLite Configuration

DB_TYPE=sqlite
DB_DATABASE=database.db

Automatic .env Management

The setup script automatically manages your database configuration. Simply change DB_TYPE in .env and run the setup script - it will automatically comment/uncomment the appropriate configuration sections!

Model Example

class Product extends Database
{
    protected string $table = 'products';

    public function getAll(): array
    {
        $sql = "SELECT * FROM {$this->table} ORDER BY created_at DESC";
        return $this->query($sql)->fetchAll();
    }

    public function findById(int $id): ?object
    {
        $sql = "SELECT * FROM {$this->table} WHERE id = :id LIMIT 1";
        return $this->query($sql, ['id' => $id])->fetch();
    }

    public function create(array $data): bool
    {
        return $this->insert($data);
    }
}

Migrations

Create and run database migrations:

# Create migration
php frame make:migration create_products_table

# Edit the migration file
# database/migrations/YYYY_MM_DD_HHMMSS_create_products_table.sql

# Run migrations
php frame migrate

# Rollback migrations
php frame migrate:rollback