eznio/db

受Laravel启发的数据库集合库

1.0.7 2017-03-08 15:35 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:09:24 UTC


README

受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();

更多信息

支持的RDBMS

目前支持SQLite和MySQL。

错误处理

特别设计为静默模式,没有内部异常。如果发生任何情况,函数返回null/[]

占位符基础

几个驱动函数接受第二个参数,称为$args。这是占位符值列表。库使用PDO占位符语法,因此我们有两种选择

  • 使用?作为占位符名称,并提供简单的替换列表

    $driver->query(
        'SELECT * FROM USERS WHERE name = ? AND age = ?',
        ['John', 26]
    );

    在这种情况下,替换顺序应与查询中的顺序相同。

  • 使用:named:占位符

    $driver->query(
        'SELECT * FROM USERS WHERE name = :name: AND age = :age:',
        ['age' => 26, 'name' => 'John']
    );

    命名替换可以以任何顺序传递。

参考

驱动程序

驱动程序是针对数据库的特定请求处理类。实现了\eznio\db\drivers\Driver接口。

query()

public function query($sql, array $args = []);

运行SQL查询,不返回任何内容。对于像"SET NAMES"这样的系统查询很有用。

$driver->query("SET NAMES :encoding:", ['encoding' => 'UTF-8'];

select()

public function select($sql, array $args = []);

运行SQL查询,并将生成的结果作为数组返回。

$result => $driver->select("SELECT * FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      ['id' => 1, 'name' => 'John', 'surname' => 'Smith'],
 *      ['id' => 2, 'name' => 'John', 'surname' => 'Doe']
 */ ];

如果结果集中存在别名ARRAY_KEY - 它的值将作为数组键添加(并从结果行中删除)

$result => $driver->select("SELECT id AS ARRAY_KEY, * FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      1 => ['name' => 'John', 'surname' => 'Smith'],
 *      2 => ['name' => 'John', 'surname' => 'Doe']
 */ ];

getRow()

public function getRow($sql, array $args = []);

运行SQL查询,并将生成的第一行作为数组返回。

$result => $driver->getRow("SELECT * FROM users WHERE name = ?", ['John']);

getColumn()

public function getColumn($sql, array $args = []);

运行SQL查询,并将生成的第一列作为单元素数组数组返回。

$result => $driver->getColumn("SELECT id FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      1,
 *      2
 */ ];

ARRAY_KEY别名也适用于此处

$result => $driver->getColumn("SELECT id AS ARRAY_KEY, surname FROM users WHERE name = ?", ['John']);

/*  $result = [
 *      1 => 'Smith',
 *      2 => 'Doe'
 */ ];

getCell()

public function getCell($sql, array $args = []);

运行SQL查询,并将生成的第一行的第一列返回

$result => $driver->getCell("SELECT COUNT(*) FROM users WHERE name = ?", ['John']);

//  $result = 2;

load()

public function load($table, $id);

通过id从给定表中获取单行值的快捷方式

$result => $driver->load('users', 1);

//  $result = ['id' => 1, name' => 'John', 'surname' => 'Smith'];

insert()

public function insert($table, array $data);

将数据插入表中,并返回插入的ID

$result => $driver->insert('users', [
    'name' => 'John',
    'surname' => 'McKey'
]);

//  $result = 3;

update()

public function update($table, $id, $data);

通过行ID更新现有数据

$driver->update('users', [
    'name' => 'Mike',
], 1);

delete()

public function delete($table, $id);

删除具有给定ID的行

$driver->delete('users', 3);

实体

集合

EntityManager