1. Go to this page and download the library: Download piko/db-record library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
piko / db-record example snippets
use Piko\DbRecord;
use Piko\DbRecord\Attribute\Table;
use Piko\DbRecord\Attribute\Column;
#[Table(name:'contact')]
class Contact extends DbRecord
{
#[Column(primaryKey: true)]
public ?int $id = null;
#[Column]
public $name = null;
#[Column]
public ?int $order = null;
}
// 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();
echo "Contact id : {$contact->id}"; // 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(...))
// Load a single record by primary key:
$contact = (new Contact($db))->load(1);
var_dump($contact->name); // John