piko/db-record

A lightweight Active Record helper built on top of PDO.

v2.0.2 2023-10-18 16:20 UTC

This package is auto-updated.

Last update: 2024-04-18 17:34:32 UTC


README

build Coverage Status

A lightweight Active Record implementation built on top of PDO.

Installation

It's recommended that you use Composer to install Piko Db.

composer require piko/db-record

Usage

require 'vendor/autoload.php';

use Piko\DbRecord;

class Contact extends DbRecord
{
    protected $tableName = 'contact';

    protected $schema = [
        'id'        => self::TYPE_INT,
        'name'      => self::TYPE_STRING,
        'order'     =>  self::TYPE_INT
    ];
}

// See https://www.php.net/manual/en/class.pdo.php
$db = new PDO('sqlite::memory:');

$query = <<<EOL
CREATE TABLE contact (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  `order` INTEGER
)
EOL;

$db->exec($query);

$contact = new Contact($db);
$contact->name = 'John';
$contact->order = 1;
$contact->save();

var_dump($contact->id); // 1

$st = $db->prepare('SELECT * FROM contact');
$st->execute();
$rows = $st->fetchAll(PDO::FETCH_CLASS, Contact::class, [$db]);

print_r($rows); // Array ([0] => Contact Object(...))

$contact = (new Contact($db))->load(1);

var_dump($contact->name); // John

$contact->delete();

print_r($st->fetchAll()); // Array()