j0113 / odb
PHP面向对象数据库包装器。设计用于(成为)兼容多个数据库。查询是用PHP编写的,表与模型相连。
v1.2.1
2021-03-29 13:42 UTC
Requires
- php: >=7.4
- ext-pdo: *
README
PHP面向对象数据库包装器。设计用于(成为)兼容多个数据库。查询是用PHP编写的,表与模型相连。
先决条件/要求
- PHP >=7.4(目前如此)
- 一个MySQL数据库(含表)
- PDO
安装
J0113/ODB可以使用composer进行安装。
composer require j0113/odb *
入门指南
1. 数据库连接
你首先想做的事情是设置与数据库的连接。这将设置整个应用程序的数据库连接。
use J0113\ODB\PDODatabase; PDODatabase::connect("server.localhost", "myusername", "mypassword", "mydatabase");
2. 映射一个模型
所有模型都必须扩展J0113\ODB\PDODatabaseObject
。
use J0113\ODB\PDODatabaseObject; class User extends PDODatabaseObject { // First you may define a table (defaults to the className) protected const TABLE = "users"; // Than some fields: protected ?string $username = null; protected ?string $firstname = null; protected ?string $lastname = null; // Maybe some relations (more about that later) protected const RELATIONS = [ "orders" => ["toOne", "Order", "user_id", "id"], ]; // And getters and setters public function getUsername() : ?string ... }
这就对了,现在你可以以面向对象的方式从PHP访问数据库了。
用法
1. 插入
$user = new User(); $user->setFirstname("John"); $user->setLastname("Smith"); $user->setUsername("john_smith"); $user->save(); echo $user->getId();
2. 获取数据
要获取数据,必须使用J0113\ODB\QueryBuilder
,如果支持更多数据库,查询也将得到支持。
use J0113\ODB\QueryBuilder; /** * Get some users and display info * @param QueryBuilder * @return User[]|null */ $users = User::get( (new QueryBuilder()) ->where("firstname", "John") ->andWhere("lastname", "Smith") ->limit(5) ); if (!empty($users)){ foreach ($users as $user){ echo "Hello " . $user->getFirstname() . "!\n"; } } /** * Get one user * @param QueryBuilder * @return User|null */ $user = User::getOne( (new QueryBuilder())->where("username", "oliver12345") );
3. 更新一行
要更新数据,过程大致相同,只需保持ID不变并使用save()
。
use J0113\ODB\QueryBuilder; /** * Get one user * @param QueryBuilder * @return User|null */ $user = User::getOne( (new QueryBuilder())->where("username", "oliver12345") ); $user->setFirstname("Oli"); $user->save();
关系
将数据库映射到PHP对象的一个问题是,当关系发挥作用时。ODB采用即时编译(JIT)方法,相关对象在PHP中是“可访问”的,但只有在被访问时才会执行查询。
关系在模型对象中使用protected const RELATIONS
数组定义,关系可以是toOne
或toMany
。
use J0113\ODB\PDODatabaseObject; use J0113\ODB\QueryBuilder; class User extends PDODatabaseObject { /** * Stores all the relations, these are read only and results in more than one query IF the relation is queried. * Must be an sub array with "key" => ["type", "class", "column", "property"]. * - Key: is under what $obj->key the relation can be accessed. * - Type: can be toOne (this-1) or toMany (this-multiple) * - Class: the class it should be connected to (must be an child of PDODatabaseObject) * - Column: the column it should search in * - Property: the value to match against, is a property (or variable) of the current object * * SQL will generated by: * - LIMIT: Type * - FROM: Class * - WHERE: column = this->property * * protected const RELATIONS = ["customer" => ["toOne", "Model\User", "id", "user_id"] ]; * $order->customer = Model\User(...) * * Tip: Use the PHPDoc '@ property' to let the IDE know about the relation. * * @var array */ protected const RELATIONS = [ "orders" => ["toMany", "Order", "user_id", "id"] // $this->orders would result in Order::get((new QueryBuilder())->where("user_id", $this->id)); ]; /** * You may use a getter * @return Order[] */ public function getOrders() : array { return $this->orders; } } $user = User::getOne((new QueryBuilder())); $user_orders = $user->getOrders();
许可
J0113/ODB
根据Apache 2.0许可发布。有关详情,请参阅附带的LICENSE
文件。