15/06
2016
Wallogit.com
2017 © Pedro Peláez
Yet another simple and consistent pdo based php-mysql query builder
An overnightly simple and consistent PHP-mysql query builder based on PDO., (*1)
composer require offworks/overnight
git clone https://github.com/offworks/overnight
then, require the autoload from the location, (*2)
require_once __DIR__.'/path/to/overnight/src/autoload.php';
$db = \Overnight\Connection::create('localhost', 'user', 'password', 'dbname');
Get an array of records, (*3)
$users = $db->table('user')
->where('created_at > ?', array(date('Y-m-d H:i:s'))
->execute()->all();
Get an associated key value single (first) record, (*4)
$user = $db->from('user')
->select('name, email, birthdate')
->where('user_id = ?', array($userId))
->execute()->first();
p/s : from() is an alias of table(), (*5)
Multiple wheres, (*6)
$tickets = $db->from('ticket')
->where('available = ? AND used = ?', array(1, 0))
->where('expiry_date < ?', array('2020-10-10'))
->andWhere('seat_type = ?', array('single'))
->orWhere('master_ticket = ?', array(1))
->execute();
// will execute something like
SELECT * FROM ticket WHERE available = 1 AND used = 0 AND expiry_date < '2020-10-10' AND seat_type = 'single'
$books = $db->from('book')
->innerJoin('author ON author.author_id = book.author_id')
->execute()->all();
$users = $db->from('user')
->leftJoin('membership ON membership.user_id = user.user_id')
->execute()->all();
Parameterized join, (*7)
$users = $db->from('book')
->innerJoin('author ON author.author_id = book.author_id AND author.is_alive = ?', array(1))
->execute()->all();
$students = $db->from('student')
->where('is_alive = ?', array(1))
->orderBy('last_seen DESC')
->execute()->all();
$sql = $db->from('news')
->innerJoin('editor ON news.editor_id = editor.editor_id')
->where('DATE(published_at) = ?', array(date('Y-m-d H:i:s')))
->orderBy('published_at DESC')
->getSql();
$db->insert('user')->values(array(
'name' => 'James',
'like' => 'Foo cake',
'birthdate' => '1977-05-09'
))->execute();
or, (*8)
$db->insert('user', array(
'name' => 'James',
'like' => 'Foo cake',
'birthdate' => '1977-05-09'
))->execute();
$userId = $db->lastInsertId();
or, (*9)
$userId = $db->insert('user')->values($values)->execute()->id();
$db->update('book')
->where('book_id = ?', array($bookId))
->set(array('title' => 'the lost marble - first edition'))
->execute();
$db->delete('author')
->where('author_id = ?', array($authorId))
->execute();
$query->execute() getStatement() $query->getSql() $db->lastInsertId() $db->execute(string $sql, array $values, array $params)) $db->getPdo()
from(string $table) or table(string $table) select(string|array $columns) where(string $condition, array $values = array()) orWhere(string $condition, array $values = array()) orderBy(string $orderBy) groupBy(string $groupBy) innerJoin(string $condition, array $values = array()) leftJoin(string $condition, array $values = array()) rightJoin(string $condition, array $values = array()) limit(int $limit, int $offset = null) having(string $condition, array $values = array()) execute()->all() execute()->first()
insert(string $table, array $values = array()) values(array $values) execute()->id()
update(string $table) set(array $data) where(string $condition, array $values = array()) orWhere(string $condition, array $values = array())
delete(string $table) where(string $condition, array $values = array()) orWhere(string $condition, array $values = array())
MIT, (*10)