figdice/gazedb
Composer 安装命令:
composer require figdice/gazedb
包简介
Simple PDO wrapper for agile yet safe ORM and direct queries
README 文档
README
Simple PDO wrapper for agile yet safe ORM and direct SQL queries for PHP
1. Abstract
Use this lib whenever you don't need a full-fledged ORM framework.
What gazedb does not provide:
- Comprehensive mapping of the full database schema
- Caching layer
- Declarative relationship syntax
- Lazy loading of collections
If you do not know how to write clean efficient SQL queries, or if you do not understand how an index can help your architecture, or if you plan on replacing your underlying DBMS vendor every other weekend, you should use a different library (such as Doctrine). You might need to learn a new querying language (Hibernate QL, Doctrine QL, etc.) and a complex family of framework-specific annotation-based grammar to declare the relationship between your tables.
What gazedb does provide:
- Simple PDO wrapper
- Normative constant-based notation for tables and columns names,
- Simple helpers for single-object CRUD operations,
- Simple syntax to assist in creating your own real SQL queries in a reusable way.
In most cases, your gazedb Model Objects do not know how they relate to each other. The database intelligence remains in your hands (indexes, best way to join, when and how to fetch, etc.).
2. Model Object
Create one class per table, as child class of ModelObject.
class Employee extends ModelObject
The subclass must implement the following methods:
tableNamemapFields
The ModelObject subclass may implement the following methods:
mapPKmapAutoIncrement
2.1. Table name
/** * Must override. Returns the name of the table. * @return string */ protected static function tableName() { return 'employees'; }
You will never use tableName() directly.
This method is used in the ancestor's table() static method.
Employee::table() returns the DB table name that you configured for Employee class.
Usage:
$query = "select * from ".Employee::table()." limit 10"
2.2. Columns
Declare Class constants for the field names of your table.
class Employee extends ModelObject { public const ID = 'employee_id'; public const LASTNAME = 'lastname'; public const SALARY = 'salary'; public const DEPARTMENT = 'dept_id';
The const are not explicitly used by the library ; rather, they are a reusability helper, for you
to write non hard-coded DB identifiers in your code and queries.
You must indicate gazedb what columns are part of the data exchange, via method mapFields:
/** * Must override. Returns the associative array of the columns mapping * (column name => default value) * @return array */ public static function mapFields() { return [ self::ID => null, self::LASTNAME => '', self::SALARY => 0, self::DEPARTMENT => null ]; }
And you must write mutators for your columns:
public function getLastname() { return $this->column(self::LASTNAME); } /** * @return Employee */ public function setLastname($value) { return $this->assign(self::LASTNAME, $value); }
Make your setters chainable by hinting the return type to same class.
2.3. Primary key
You can specify a primary key (incl. a multi-column one) by overriding the method:
/** * Should override, if the table has a PK (single- or multiple-field). * Returns an array of field names. * @return array */ public function mapPK()
You never need to invoke this method explicitly.
A typical example of mapPK implementation is:
public function mapPK() { return [ self::ID ]; }
2.4. Auto-increment
Provide an implementation for mapAutoIncrement if your table has an auto-numbering column which the
database assigns alone at insertion time.
/** * The name of the auto-increment column. * @return string */ public function mapAutoIncrement() { return self::ID; }
3. Build Your Queries
You write typical queries in your code by taking benefit from the const names.
$query = " select Employee.".Employee::ID.", Employee.".Employee::LASTNAME.", Dept.".Department::FLOOR." from ".Employee::table()." Employee inner join ".Department::table()." Dept on Dept.".Department::ID." = Employee.".Employee::DEPARTMENT." where Employee.".Employee::SALARY." > 10000 ";
So, you write real, valid SQL, in the target dialect of your choice (i.e. specific syntax and functions of the actual SQL engine).
You may use the usual parameter binding that comes with PDO (the :param syntax) and
you should take care of SQL injection protection.
The only benefit you gain from writing your queries using the above recommendation, is consistency and syntax checking on the names of tables and fields.
4. Database object
gazedb lets you handle several simultaneous connections to different data sources. It uses the injectable Singleton pattern, with named instances.
$db = Database::get(); $archiveDB = Database::get('archive');
You must invoke injectDsn($dsnString, $username, $password) on a Database instance, to specify the PDO connection string.
Then, you manipulate the underlying PDO instance directly, with:
$db->pdo()
which returns your plain and well-known PDO object, for you to execute:
$recordset = $db->pdo()->query($query);
4.1. CRUD operations
In addition to the underlying PDO operations, made directly on the $database->pdo() instance, gazedb offers simple CRUD auto-mapping methods for those Model objects that map a primary key.
4.1.1. Load
// "new" does not create anything in DB $employee = new Employee(); // Specify the key value for the employee you wish to load: $employee->setId(12); // Fetch the record and auto-set all the mapped columns (see mapFields() method) $database->load($employee); echo $employee->getLastname();
In case the record could not be found for specified primary key, the Database::load method
will throw an ObjectNotFoundException.
4.1.2. Update
Once you have a populated object, you can modify any field with its mutators, and save it back
to the database with the udpate method.
$employee->setFloor(8); $database->update($employee);
The update method produces a query which only changes the values you modified explicitly,
leaving all the other fields untouched.
Notice: gazedb does not check the state of the object before storing it back to database. If the record was changed by another process or connection, your udpate statement will still be issued without your knowing it.
Likewise, update does not re-fetch the object's fields if they were changed in database since
your previous call to load.
4.1.3. Insert
$employee = new Employee(); $employee ->setLastname('Smith') ->setFloor(10) ->setSalary(60000); $database->insert($employee); echo $employee->getId();
The insert method fires an insert query, optionally assigning the auto-increment value
to the mapped auto-increment field if you specified one in your Model object.
After the insert call, your object is considered "clean" and you can modify its values again
through the mutators, before performing an update on the modified values only.
figdice/gazedb 适用场景与选型建议
figdice/gazedb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 785 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「query」 「pdo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 figdice/gazedb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 figdice/gazedb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 figdice/gazedb 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Query filtering in your frontend
Anax Database Active Record module for model classes.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 785
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2015-10-03