Search by

kodhe / database

karyakode

Kodhe Database - Query Builder, ORM & Drivers

Package info

github.com/kodhe/database

pkg:composer/kodhe/database

Statistics

Installs: 13

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

1.1.0 2026-09-28 18:12 UTC

This package is not auto-updated.

Last update: 2026-10-28 13:30:55 UTC


README

A refactored version of the CodeIgniter 3 database system: multi-driver connections, a fluent query builder, a Model-based ORM, and migrations. PSR-4 namespace: Kodhe\Framework\Database\.

Sub-components:

  • DB — static facade (DB::table(), DB::model(), transactions, raw queries)
  • Builder — fluent query builder (where, join, orderBy, paginate, etc.)
  • ORM\Model / Model — active-record style models + relationships in ORM/Relationships
  • Connection\ — ConnectionManager and per-engine drivers (MySQL, PostgreSQL, SQLite, etc.)
  • Migrations\ — versioned migration schema

Installation

composer require kodhe/database

Requirements: PHP >= 8.1 + the relevant PDO/driver extensions (ext-pdo_mysql, ext-pdo_pgsql, ext-pdo_sqlite).

Quick Start

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Kodhe\Framework\Database\DB;

// Query builder via the static facade
$users = DB::table('users')
    ->where('active', 1)
    ->orderBy('name', 'asc')
    ->take(10)
    ->get();

foreach ($users as $u) {
    echo $u->name . PHP_EOL;
}

Usage

1. Query Builder

$rows = DB::table('orders')
    ->select(['id', 'total'])
    ->whereBetween('created_at', ['2026-01-01', '2026-12-31'])
    ->whereIn('status', ['paid', 'shipped'])
    ->orWhere('express', true)
    ->paginate(perPage: 20, page: 2);

Method utama Builder: where, orWhere, whereIn, whereNotIn, whereNull, whereNotNull, whereBetween, whereLike, select, join, leftJoin, rightJoin, orderBy, groupBy, having, take, skip, with, get, first, count, paginate.

2. Model (Active Record)

use Kodhe\Framework\Database\Model;

class Product extends Model
{
    // The default table name is derived from the class name (Product -> products)
}

$product  = Product::find(1);
$allNew   = Product::where('stock', '>', 0)->get();

3. Transactions

DB::beginTransaction();
try {
    DB::table('accounts')->where('id', 1)->update(...);
    DB::table('accounts')->where('id', 2)->update(...);
    DB::commit();
} catch (\Throwable $e) {
    DB::rollback();
    throw $e;
}

4. Raw queries with bindings

$result = DB::raw('SELECT * FROM logs WHERE level = ? AND created_at > ?', ['error', '2026-01-01']);

Struktur Direktori

src/
├── DB.php            # Static facade
├── Builder.php       # Fluent query builder
├── Model.php         # Model modern (extends ORM\LegacyModel)
├── Loader.php        # CI3-compatible configuration/connection loader
├── Connection/       # ConnectionInterface, ConnectionManager, Drivers/
├── ORM/              # Model, LegacyModel, CI_Model, Relationships/, README.md
└── Migrations/       # Migration classes & schema

CodeIgniter 3 Compatibility

  • The legacy $this->db->get('table') style is still served through the Loader/CI_Model shims in ORM/.
  • Connection configuration uses CI3 array keys (hostname, username, database, dbdriver, etc.).

Notes & Limitations

  • The example queries above assume an active connection has been configured via ConnectionManager/Loader; see database/src/Connection/Drivers for the supported engines.
  • The old document at doc/database_README.md summarizes an early version; this file is the package's primary reference.

Testing

cd database && composer install && vendor/bin/phpunit

License

MIT. See LICENSE.