Database

API Documentation

Build Coverage

Database Library documentation

Usage Examples

Creating tables

Create a Users table:

use Framework\Database\Database;
use Framework\Database\Definition\Table\TableDefinition;

$db = new Database('root', 'password', 'tests');

$result = $db->createTable()
	->table('Users')
	->definition(function (TableDefinition $definition) {
		$definition->column('id')->int(11)->unsigned()->autoIncrement()->primaryKey();
		$definition->column('name')->varchar(64);
		$definition->column('email')->varchar(255);
		$definition->column('active')->tinyint(1);
		$definition->column('createdAt')->datetime();
		$definition->column('updatedAt')->datetime();
	})
	->run();
CREATE TABLE `Users` (
  `id` int(11) unsigned AUTO_INCREMENT NOT NULL PRIMARY KEY,
  `name` varchar(64) NOT NULL,
  `email` varchar(255) NOT NULL,
  `active` tinyint(1) NOT NULL,
  `createdAt` datetime NOT NULL,
  `updatedAt` datetime NOT NULL
)

Create the Posts table with a foreign key userId

$result = $db->createTable()
	->table('Posts')
	->definition(function (TableDefinition $definition) {
		$definition->column('id')->int(11)->unsigned()->autoIncrement()->primaryKey();
		$definition->column('userId')->int(11)->unsigned();
		$definition->column('title')->varchar(255);
		$definition->column('content')->text();
		$definition->column('createdAt')->datetime();
		$definition->column('updatedAt')->datetime();
		$definition->index()->foreignKey('userId')->references('Users', 'id');
	})
	->run();
CREATE TABLE `Posts` (
  `id` int(11) unsigned AUTO_INCREMENT NOT NULL PRIMARY KEY,
  `userId` int(11) unsigned NOT NULL,
  `title` varchar(255) NOT NULL,
  `content` text NOT NULL,
  `createdAt` datetime NOT NULL,
  `updatedAt` datetime NOT NULL,
  FOREIGN KEY (`userId`) REFERENCES `Users` (`id`)
)

Selecting data

$result = $db->select('Country')->orderBy('iso')->run();
SELECT
 *
 FROM `Country`
 ORDER BY `iso`