An expressive SQL query builder in PHP.
An expressive SQL query builder in PHP. This package is previously known as Windstorm.
I tried to unify Doctrine and Eloquent into a single interface for them to be swappable. Unfortunately the implementation is not possible because of the different core design patterns (data mapper for Doctrine while active record for Eloquent). I realized later that the one thing common for both is their query builder and it was also common on all existing ORM packages and SQL query builders.
Install Ezekiel through Composer:
$ composer require rougin/ezekielUse the Query class to create SQL queries:
use Rougin\Ezekiel\Query;
$query = (new Query)
->select(array('u.id', 'u.name', 'p.name as position'))
->from('users u')
->leftJoin('positions p')->on('p.id', 'u.position_id')
->where('u.name')->like('%winds%')
->having('u.id')->greaterThan(0)
->orderBy('u.created_at')->desc();
// SELECT u.id, u.name, p.name as position
// FROM users u
// LEFT JOIN positions p ON p.id = u.position_id
// WHERE u.name LIKE ? HAVING u.id > ?
// ORDER BY u.created_at DESC
$sql = $query->toSql();
// array('name' => '%winds%', 'id' => 0)
$binds = $query->getBinds();After creating the query, use the Result class to return its contents:
use Rougin\Ezekiel\Query;
use Rougin\Ezekiel\Result;
$query = (new Query)
->select(array('u.id', 'u.name'))
->from('users', 'u')
->where('name')->like('%winds%')
->orderBy('created_at')->desc();
$pdo = /** returns a PDO instance */;
$result = new Result($pdo);
$items = $result->items($query);
echo json_encode($items);[
{
"id": 2,
"name": "Windsor",
"created_at": "2018-10-15 23:09:47",
"updated_at": null
},
{
"id": 1,
"name": "Windstorm",
"created_at": "2018-10-15 23:06:28",
"updated_at": null
},
{
"id": 3,
"name": "Windsy",
"created_at": "2018-10-15 23:14:45",
"updated_at": null
}
]For returning only one item from the result, use the first method instead:
// ...
use Rougin\Ezekiel\Result;
// ...
$result = new Result($pdo);
$items = $result->first($query);
echo json_encode($items);{
"id": 2,
"name": "Windsor",
"created_at": "2018-10-15 23:09:47",
"updated_at": null
}For mapping query results into an entity object, the entity can be extended to the Entity class:
// src/Entities/User.php
namespace Test\Entities;
use Rougin\Ezekiel\Entity;
class User extends Entity
{
protected $id;
protected $name;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
}If an Entity is passed to the Result class, the results will be automatically to new instances of that Entity:
use Rougin\Ezekiel\Query;
use Rougin\Ezekiel\Result;
use Test\Entities\User;
$user = (new User)
->select('id, name')->from('users')
->where('name')->equals('Windsor');
$pdo = /** returns a PDO instance */ ;
$result = new Result($pdo);
/** @var \Rougin\Ezekiel\Fixture\Entities\User[] */
$users = $result->items($user);
foreach ($users as $user)
{
echo 'Hello ' . $user->getName() . '!<br>';
}All available SQL statements should be supported by Ezekiel. These includes DELETE FROM, INSERT INTO, SELECT, and UPDATE:
use Rougin\Ezekiel\Query;
$query = (new Query)
->deleteFrom('users')
->where('id')->equals(12);
// DELETE FROM users WHERE id = ?
$sql = $query->toSql();
// array('id' => 12)
$binds = $query->getBinds();use Rougin\Ezekiel\Query;
$query = (new Query)
->insertInto('users')
->values(array('name' => 'Ezekiel', 'age' => 20));
// INSERT INTO users (name, age) VALUES (?, ?)
$sql = $query->toSql();
// array('name' => 'Ezekiel', 'age' => 20)
$binds = $query->getBinds();$query = (new Query)
->select(array('u.id', 'u.name'))
->from('users u')
->where('u.name')->like('%winds%')
->orderBy('u.created_at')->desc();
// SELECT u.id, u.name FROM users u
// WHERE u.name LIKE ?
// ORDER BY u.created_at DESC
$sql = $query->toSql();
// array('name' => '%winds%')
$binds = $query->getBinds();use Rougin\Ezekiel\Query;
$query = (new Query)
->update('users')
->set('name', 'Ezekiel')
->where('id')->equals(12);
// UPDATE users SET name = ? WHERE id = ?
$sql = $query->toSql();
// array('name' => 'Ezekiel', 'id' => 12)
$binds = $query->getBinds();WindstormAs being renamed from Windstorm, this will introduce backward compatibility (BC) breaks through out the source code. This was done to increase extensibility, simplicity and maintainbility and was discussed in one of my blog post which aims to solve overengineering of my own open source packages:
I also want to extend this plan to my personal packages as well like Staticka and Transcribe. With this, I will introduce backward compatibility breaks to them initially as it is hard to migrate their codebase due to minimal to no documentation being provided in its basic usage and its internals. As I checked their code, I realized that they are also over engineered, which is a mistake that I needed to atone for when updating my packages in the future.
Please see the UPGRADING page for the specified breaking changes.