Database query organisation.
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

Database logic organisation for PHP 7 projects.

Database API organisation for PHP 7 projects.

Encloses your application's database scripts within a simple and standardised interface, separating database access from your page logic.


Build status Code quality Code coverage Current version PHP.Gt/Database documentation

Example usage

This library organises SQL access through a consistent API. To execute an example query located at src/query/shop_item/getItemsInCategory.sql, the following pattern is used:

$user = $db->fetch("user/getById", 105);

Examples of CRUD operations:

// "fetchAll" method returns an array of Row objects, optionally constructed as 
// a custom type with well-typed properties and helper methods.

$bookList = $db->fetchAll("shopitem/getItemsInCategory", "books");

foreach($bookList as $book) {
	echo "Book title: " . $book->title . PHP_EOL;
	echo "Book price: " . $book->price . PHP_EOL;
	
	if($book->offerEnds) {
		echo "Item on offer until: " . $book->offerEnds->format("dS M Y");
	}
}

// "Create" method always returns the last inserted ID:
$newCustomerId = $db->create("customer/new", [
	"first_name" => "Marissa",
	"last_name" => "Mayer",
	"dob" => new DateTime("1975-05-30"),
]);

// "Update" or "delete" methods always return the number of affected rows:
$numberOfItemsAffected = $db->update("shop/item/increasePrice", [
	"percent" => 12.5
	"max_increase" => 20.00
]);

$numberOfDeletedReviews = $db->delete(
	"remove/deleteOlderThan",
	new DateTime("-6 months")
);

Features at a glance

Compatible database engines

Compatibility is provided for the following database providers:

  • MySQL
  • Postgres
  • SQLite
  • SQL Server
  • Mongo (planned)
  • CouchDB (planned)