PHP-MySQLi-Database-Class 中的 dbObject 使用指南

PHP-MySQLi-Database-Class 中的 dbObject 使用指南

PHP-MySQLi-Database-Class Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements. PHP-MySQLi-Database-Class 项目地址: https://gitcode.com/gh_mirrors/ph/PHP-MySQLi-Database-Class

概述

dbObject 是基于 MysqliDb 库实现的一个轻量级模型层封装。它并非一个全功能的 ORM 系统,而是为 MysqliDb 提供了一个面向对象的包装层,使数据库操作更加便捷和直观。

核心特性

  • 简洁的模型定义方式
  • 支持基本的 CRUD 操作
  • 提供关联关系处理
  • 内置数据验证机制
  • 支持时间戳自动维护
  • 数组/JSON 字段处理
  • 分页支持
  • 字段隐藏功能

安装与初始化

要使用 dbObject,首先需要包含相关类文件:

require_once("libs/MysqliDb.php");
require_once("libs/dbObject.php");

// 初始化数据库连接
$db = new Mysqlidb('localhost', 'user', '', 'testdb');
// 启用模型自动加载
dbObject::autoload("models");

模型定义

基本模型定义

每个数据库表都可以映射为一个 dbObject 实例。定义模型非常简单:

class User extends dbObject {}

默认情况下,类名会转换为小写形式作为表名。如果需要指定不同的表名:

protected $dbTable = "users";

快速创建模型实例

对于简单的表操作,可以不创建模型类,直接使用 table() 方法:

$user = dbObject::table("users");

数据操作

查询数据

获取所有记录
$users = User::get();
foreach ($users as $u) {
    echo $u->login;
}
条件查询
$users = User::where("login", "demo")->get(Array (10, 20));
通过主键查询
$user = User::byId(1);
echo $user->login;

可以自定义主键字段名:

protected $primaryKey = "userId";

插入数据

对象方式插入
$user = new User;
$user->login = 'demo';
$user->password = 'demo';
$id = $user->save();
数组方式插入
$data = Array('login' => 'demo', 'password' => 'demo');
$user = new User($data);
$id = $user->save();

更新数据

$user = User::byId(1);
$user->password = 'demo2';
$user->save();

或者:

$data = Array('password', 'demo2');
$user = User::byId(1);
$user->save($data);

删除数据

$user = User::byId(1);
$user->delete();

关联关系

dbObject 支持两种关联关系:hasOnehasMany

一对一关系 (hasOne)

protected $relations = Array(
    'person' => Array("hasOne", "person", 'id')
);

// 使用
$user = User::byId(1);
echo $user->person->firstName;

一对多关系 (hasMany)

protected $relations = Array(
    'products' => Array("hasMany", "product", 'userid')
);

// 使用
$user = User::byId(1);
foreach ($user->products as $p) {
    echo $p->title;
}

优化关联查询

使用 with() 方法可以减少查询次数:

$user = User::with('person')->byId(1);

高级功能

时间戳自动维护

protected $timestamps = Array ('createdAt', 'updatedAt');

数组字段处理

JSON 格式存储
protected $jsonFields = Array('options');
管道分隔格式存储
protected $arrayFields = Array('sections');

数据验证

定义验证规则:

protected $dbFields = Array(
    'login' => Array('text', 'required'),
    'password' => Array('text'),
    'createdAt' => Array('datetime'),
    'updatedAt' => Array('datetime'),
    'custom' => Array('/^test/'),
);

处理验证错误:

if (!$id = $user->save()) {
    print_r($user->errors);
    echo $db->getLastError();
}

返回数组或 JSON

返回数组
$user = User::ArrayBuilder()->byId(1);
echo $user['login'];
返回 JSON
$userJson = User::JsonBuilder()->with("product")->byId(1);

分页支持

$page = 1;
Product::$pageLimit = 2;
$products = Product::arraybuilder()->paginate($page);
echo "showing $page out of " . Product::$totalPages;

隐藏字段

protected $hidden = array('password', 'token');

最佳实践

  1. 对于复杂应用,建议为每个表创建专门的模型类
  2. 使用关联关系时,考虑使用 with() 方法优化查询性能
  3. 重要字段使用 hidden 属性保护敏感数据
  4. 充分利用验证机制确保数据完整性
  5. 对于频繁查询的数据,考虑使用缓存机制

dbObject 提供了一个简单而强大的方式来操作数据库,既保持了灵活性又不失便捷性,非常适合中小型项目的快速开发。

PHP-MySQLi-Database-Class Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements. PHP-MySQLi-Database-Class 项目地址: https://gitcode.com/gh_mirrors/ph/PHP-MySQLi-Database-Class

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

劳妍沛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值