定制 eznio/db 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eznio/db

Composer 安装命令:

composer require eznio/db

包简介

DB Collections library inspired by Laravel

README 文档

README

DB Entity Collections library inspired by Laravel

Quickstart

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

More details

Supported RDBMS

SQLite and MySQL for now.

Error handling

Especially designed to be silent, no internal exceptions. Functions return null/[] if anything happens.

Placeholders basics

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.

Reference

Driver

Driver is DB-specific request handling class. Implements the \eznio\db\drivers\Driver interface.

query()

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'];

select()

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']
 */ ];

getRow()

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']);

getColumn()

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'
 */ ];

getCell()

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;

load()

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'];

insert()

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;

update()

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

Updates existing data by row ID:

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

delete()

public function delete($table, $id);

Deletes row with given ID:

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

Entity

Collection

EntityManager

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固