Introducing Phrap

Phrap

Phrap is a super-light PHP database wrapper using PDO for basic CRUD operations with MySQL. If that doesn’t sound sexy, well, that’s because it isn’t.

I often write light web-service type apps that require simple DB access, but are far to simple to spin up a big ORM like Doctrine. Phrap is a tiny library (script) that provides simple read/write/delete to the DB using PDO to handle escaping and such.

It reads, it writes, it deletes. It doesn't do associations, joins or validations, unless you feel like forking and adding. It keeps you from having to write a lot of SELECT * FROM blah blah blah and $stmt->prepare() every time you need something from your database, yet its stupid small so it doesn't drag on your resources (much).

This was something I needed for myself but I also used this little project to teach myself PHPUnit, and yes, I now see some of the benefits of testing your software.

Speaking of which, read the tests to get a better idea how it works.

Get it Fresh on the GitHubs

A little Phrap:

<?php
class FileUpload extends Model
{
    public $table = "files";
    public $id;
    public $filename;
    public $userid;
    public $email;
 }

// Create
$dbh  = new DB($database);
$file = new FileUpload($dbh);
$file->create();
$file->set(array('filename' => 'heffalump.txt', 'codswoddle' => 'phiminster'));
$file->save();

// Query
$result = $file->find('first');
$result = $file->find('all');
$result = $file->find('first', array('email' => 'dazza@email.com'));
$result = $file->query(
     'SELECT * FROM files WHERE email = :email ORDER BY userid LIMIT 1',
     array(':email' => 'dazza@email.com')
 );

// Delete manually by id
$file->delete(1);

// Set id in object then delete it
$file->find('first');
$file->delete();
?>

Comments