Package Data | |
---|---|
Maintainer Username: | evgeny-zinder |
Package Create Date: | 2016-07-04 |
Package Last Update: | 2017-03-08 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:22:55 |
Package Statistics | |
---|---|
Total Downloads: | 42 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
DB Entity Collections library inspired by Laravel
// Initializing database-specific driver
$driver = new \eznio\db\drivers\Sqlite('db/test.sqlite3');
// Creating Entity Manager
$em = new \eznio\db\EntityManager($driver);
// Getting query repository by table name
$repo = $em->getRepository('test');
// Getting ActiveRecord entity of table row with id = 1
$entity =$repo->findOneById(1);
// Updating and saving entity
$entity->field = 'new value';
$entity->save();
SQLite and MySQL for now.
Especially designed to be silent, no internal exceptions. Functions return null
/[]
if anything happens.
Several driver's functions accept second parameter called $args
. This is list of placeholder values. Library is using PDO placeholders syntax, so we have 2 options:
use ?
as placeholder name and provide simple list of substitutions:
$driver->query(
'SELECT * FROM USERS WHERE name = ? AND age = ?',
['John', 26]
);
In this case, substitutions order should be the same as in query.
use :named:
placeholders:
$driver->query(
'SELECT * FROM USERS WHERE name = :name: AND age = :age:',
['age' => 26, 'name' => 'John']
);
Named substitutions can be passed in any order.
Driver is DB-specific request handling class. Implements the \eznio\db\drivers\Driver
interface.
public function query($sql, array $args = []);
Runs SQL query and doesn't return anything. Useful for system queries like "SET NAMES" or similar.
$driver->query("SET NAMES :encoding:", ['encoding' => 'UTF-8'];
public function select($sql, array $args = []);
Runs SQL query and returns produced result as array.
$result => $driver->select("SELECT * FROM users WHERE name = ?", ['John']);
/* $result = [
* ['id' => 1, 'name' => 'John', 'surname' => 'Smith'],
* ['id' => 2, 'name' => 'John', 'surname' => 'Doe']
*/ ];
If ARRAY_KEY
alias exists in resulting set - it's value will be added as array keys (and removed from resulting rows):
$result => $driver->select("SELECT id AS ARRAY_KEY, * FROM users WHERE name = ?", ['John']);
/* $result = [
* 1 => ['name' => 'John', 'surname' => 'Smith'],
* 2 => ['name' => 'John', 'surname' => 'Doe']
*/ ];
public function getRow($sql, array $args = []);
Runs SQL query and returns produced its first row as array.
$result => $driver->getRow("SELECT * FROM users WHERE name = ?", ['John']);
public function getColumn($sql, array $args = []);
Runs SQL query and returns produced its first column as array of one-element arrays.
$result => $driver->getColumn("SELECT id FROM users WHERE name = ?", ['John']);
/* $result = [
* 1,
* 2
*/ ];
ARRAY_KEY
alias also works here:
$result => $driver->getColumn("SELECT id AS ARRAY_KEY, surname FROM users WHERE name = ?", ['John']);
/* $result = [
* 1 => 'Smith',
* 2 => 'Doe'
*/ ];
public function getCell($sql, array $args = []);
Runs SQL query and returns produced its first column of its first row:
$result => $driver->getCell("SELECT COUNT(*) FROM users WHERE name = ?", ['John']);
// $result = 2;
public function load($table, $id);
Shortcut to get values of a single row from given table by id:
$result => $driver->load('users', 1);
// $result = ['id' => 1, name' => 'John', 'surname' => 'Smith'];
public function insert($table, array $data);
Inserts data into the table and returns inserted ID
$result => $driver->insert('users', [
'name' => 'John',
'surname' => 'McKey'
]);
// $result = 3;
public function update($table, $id, $data);
Updates existing data by row ID:
$driver->update('users', [
'name' => 'Mike',
], 1);
public function delete($table, $id);
Deletes row with given ID:
$driver->delete('users', 3);