自己写的一个简易的ORM类,给感兴趣的朋友提供一点思路。
自己写的一个简易的ORM类,给感兴趣的朋友提供一点思路。借鉴了一点TP的思路。<?php <br>
/**<br>
* author: NickBai<br>
* createTime: 2016/11/28 0028 下午 4:00<br>
*/<br>
class MyOrm implements ArrayAccess<br>
{<br>
public $host = '127.0.0.1'; //数据库地址<br>
public $dbname = 'test'; //数据库名<br>
public $user = 'root'; //数据库用户名<br>
public $pwd = 'root'; //数据库密码<br>
public $port = '3306'; //数据库端口<br>
public $charset = 'utf8'; //数据库编码<br>
private $conn = null; //数据库链接资源<br>
private $alias = []; //记录全局的语句参数<br>
private $sql; //存储最后一条sql<br><br>
public function __construct()<br>
{<br>
if( is_null( $this->conn ) ){<br><br>
$dsn = "mysql:host=$this->host;dbname=$this->dbname;charset=$this->charset;port=$this->port";<br>
$this->conn = new PDO( $dsn, $this->user, $this->pwd );<br>
}<br>
}<br><br>
//field语句<br>
public function field( $field )<br>
{<br>
if( !is_string( $field ) ){<br>
throw new exception("field语句的参数必须为字符串");<br>
}<br><br>
$this->alias['field'] = $field;<br>
return $this;<br>
}<br><br>
//table语句<br>
public function table( $table )<br>
{<br>
if( !is_string( $table ) ){<br>
throw new exception("table语句的参数必须为字符串");<br>
}<br><br>
$this->alias['table'] = $table;<br>
return $this;<br>
}<br><br>
//where语句<br>
public function where( $where )<br>
{<br>
$this->alias['where'] = '';<br>
if( is_array( $where ) ){<br><br>
foreach( $where as $key=>$vo ){<br>
$this->alias['where'] .= " `$key`" . ' = ' . $vo . ' and ';<br>
}<br>
$this->alias['where'] = rtrim( $this->alias['where'], 'and ' );<br><br>
}else if( is_string( $where ) ){<br><br>
$this->alias['where'] = $where;<br>
}else{<br><br>
throw new exception("where语句的参数必须为数组或字符串");<br>
}<br><br>
return $this;<br>
}<br><br>
//limit语句<br>
public function limit( $limit )<br>
{<br>
$this->alias['limit'] = '';<br>
if( is_numeric( $limit ) ){<br>
$this->alias['limit'] = '0,' . $limit;<br>
}else if( is_string( $limit ) ){<br>
$this->alias['limit'] = $limit;<br>
}else{<br>
throw new exception("limit语句的参数必须为数字或字符串");<br>
}<br><br>
return $this;<br>
}<br><br>
//order语句<br>
public function order( $order )<br>
{<br>
if( !is_string( $order ) ){<br>
throw new exception("order语句的参数必须为字符串");<br>
}<br><br>
$this->alias['order'] = $order;<br>
return $this;<br>
}<br><br>
//group语句<br>
public function group( $group )<br>
{<br>
if( !is_string( $group ) ){<br>
throw new exception("group语句的参数必须为字符串");<br>
}<br><br>
$this->alias['group'] = $group;<br>
return $this;<br>
}<br><br>
//解析查询sql语句<br>
public function ParseSelectSql()<br>
{<br>
$this->sql = 'select *';<br>
if( !empty( $this->alias['field'] ) ){<br>
$this->sql = str_replace( '*', $this->alias['field'], $this->sql );<br>
}<br><br>
if( empty( $this->alias['table'] ) ){<br>
throw new exception("请用table子句设置查询表");<br>
}else{<br><br>
$this->sql .= ' from ' . $this->alias['table'];<br>
}<br><br>
if( !empty( $this->alias['where'] ) ){<br>
$this->sql .= ' where ' . $this->alias['where'];<br>
}<br><br>
if( !empty( $this->alias['group'] ) ){<br>
$this->sql .= ' group by ' . $this->alias['group'];<br>
}<br><br>
if( !empty( $this->alias['order'] ) ){<br>
$this->sql .= ' order by ' . $this->alias['order'];<br>
}<br><br>
if( !empty( $this->alias['limit'] ) ){<br>
$this->sql .= ' limit ' . $this->alias['limit'];<br>
}<br><br>
}<br><br>
//解析添加sql语句<br>
public function ParseAddSql()<br>
{<br>
$this->sql = 'insert into ';<br>
if( empty( $this->alias['table'] ) ){<br>
throw new exception("请用table子句设置添加表");<br>
}else{<br><br>
$this->sql .= $this->alias['table'] . ' set ';<br>
}<br><br>
return $this->sql;<br>
}<br><br>
//解析更新sql语句<br>
public function ParseUpdateSql()<br>
{<br>
$this->sql = 'update ';<br>
if( empty( $this->alias['table'] ) ){<br>
throw new exception("请用table子句设置修改表");<br>
}else{<br><br>
$this->sql .= $this->alias['table'] . ' set ';<br>
}<br><br>
if( empty( $this->alias['where'] ) ){<br>
throw new exception("更新语句必须有where子句指定条件");<br>
}<br><br>
return $this->sql;<br>
}<br><br>
//解析删除sql语句<br>
public function ParseDeleteSql()<br>
{<br>
$this->sql = 'delete from ';<br>
if( empty( $this->alias['table'] ) ){<br>
throw new exception("请用table子句设置删除表");<br>
}else{<br><br>
$this->sql .= $this->alias['table'];<br>
}<br><br>
if( empty( $this->alias['where'] ) ){<br>
throw new exception("删除语句必须有where子句指定条件");<br>
}<br><br>
$this->sql .= ' where ' . $this->alias['where'];<br><br>
return $this->sql;<br>
}<br><br><br>
//查询语句<br>
public function select()<br>
{<br>
$this->ParseSelectSql();<br>
$row = $this->conn->query( $this->sql )->fetchAll( PDO::FETCH_ASSOC );<br>
$result = [];<br><br>
foreach( $row as $key=>$vo ){<br><br>
$arrObj = clone $this; //clone当前对象防止对this对象造成污染<br>
$arrObj->data = $vo;<br>
$result[$key] = $arrObj;<br>
unset( $arrObj );<br>
}<br><br>
return $result;<br>
}<br><br>
//查询一条<br>
public function find()<br>
{<br>
$this->ParseSelectSql();<br>
$row = $this->conn->query( $this->sql )->fetch( PDO::FETCH_ASSOC );<br><br>
$arrObj = clone $this; //clone当前对象防止对this对象造成污染<br>
$arrObj->data = $row;<br>
$result = $arrObj;<br>
unset( $arrObj );<br><br>
return $result;<br>
}<br><br>
//添加数据<br>
public function add( $data )<br>
{<br>
if( !is_array( $data ) ){<br>
throw new exception("添加数据add方法参数必须为数组");<br>
}<br><br>
$this->ParseAddSql();<br>
foreach( $data as $key=>$vo ){<br>
$this->sql .= " `{$key}` = '" . $vo . "',";<br>
}<br><br>
$this->conn->exec( rtrim( $this->sql, ',' ) );<br>
return $this->conn->lastInsertId();<br>
}<br><br>
//更新语句<br>
public function update( $data )<br>
{<br>
if( !is_array( $data ) ){<br>
throw new exception("更新数据update方法参数必须为数组");<br>
}<br><br>
$this->ParseUpdateSql();<br>
foreach( $data as $key=>$vo ){<br>
$this->sql .= " `{$key}` = '" . $vo . "',";<br>
}<br><br>
$this->sql = rtrim( $this->sql, ',' ) . ' where ' . $this->alias['where'];<br>
return $this->conn->exec( $this->sql );<br><br>
}<br><br>
//删除语句<br>
public function delete()<br>
{<br>
$this->ParseDeleteSql();<br>
return $this->conn->exec( $this->sql );<br>
}<br><br>
//获取查询数据<br>
public function getData()<br>
{<br>
return $this->data;<br>
}<br><br>
//获取最后一次执行的sql语句<br>
public function getLastSql()<br>
{<br>
return $this->sql;<br>
}<br><br>
public function __get($name)<br>
{<br>
return $this->getData()[$name];<br>
}<br><br>
public function offsetExists($offset)<br>
{<br>
if( !isset( $this->getData()[$offset] ) ){<br>
return NULL;<br>
}<br>
}<br><br>
public function offsetGet($offset)<br>
{<br>
return $this->getData()[$offset];<br>
}<br><br>
public function offsetSet($offset, $value)<br>
{<br>
return $this->data[$offset] = $value;<br>
}<br><br>
public function offsetUnset($offset)<br>
{<br>
unset( $this->data[$offset] );<br>
}<br>
}你可以这么用:$orm = new MyOrm();<br><br>
//查询语句<br>
$res = $orm->table('user')->order('id desc')->select();<br>
$res = $orm->table('user')->where("name='test'")->order('id desc')->select();<br>
$res = $orm->table('user')->where(['id' => 1])->order('id desc')->find();<br>
$res = $orm->table('user')->where("age > 20")->group('group by name')->order('id desc')->limit(2)->select();<br>
$res = $orm->table('user')->where("age > 20")->group('group by name')->order('id desc')->limit('2,2')->select();<br><br>
//你可以这样处理数据<br>
foreach( $res as $key=>$vo ){<br>
echo $vo->name . '<br>';<br>
}<br>
//也可以这样处理<br>
foreach( $res as $key=>$vo ){<br>
echo $vo['name'] . '<br>';<br>
}<br>
//还可以这样<br>
foreach( $res as $key=>$vo ){<br>
print_r( $vo->getData() ) . '<br>';<br>
}<br><br>
//添加数据<br>
$data = [<br>
'name' => 'test1',<br>
'age' => 20,<br>
'password' => '21232f297a57a5a743894a0e4a801fc3',<br>
'salt' => 'domain'<br>
];<br>
$res = $orm->table('user')->add( $data );<br><br>
//更新数据<br>
$res = $orm->table('user')->where(['id' => 4])->update( ['name' => 'sdfdsfdsd', 'salt' => '111'] );<br><br>
//删除数据<br>
$res = $orm->table('user')->where(['id' => 7, 'id' => 6])->delete();<br><br>
//获取执行的sql语句<br>
echo $orm->getLastSql();<br><br>
var_dump($res);
0
0
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。
6
2026.02.28
Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。
6
2026.02.28
Go语言以其高效的并发模型和优异的性能表现广泛应用于高并发、高性能场景。其运行时机制包括 Goroutine 调度、内存管理、垃圾回收等方面,深入理解这些机制有助于编写更高效稳定的程序。本专题将系统讲解 Golang 的性能分析工具使用、常见性能瓶颈定位及优化策略,并结合实际案例剖析 Go 程序的运行时行为,帮助开发者掌握构建高性能应用的关键技能。
8
2026.02.28
本专题系统讲解 Golang 并发编程模型,从语言级特性出发,深入理解 goroutine、channel 与调度机制。结合工程实践,分析并发设计模式、性能瓶颈与资源控制策略,帮助将并发能力有效转化为稳定、可扩展的系统性能优势。
14
2026.02.27
本专题深入剖析 Golang 的高级特性与工程级最佳实践,涵盖并发模型、内存管理、接口设计与错误处理策略。通过真实场景与代码对比,引导从“可运行”走向“高质量”,帮助构建高性能、可扩展、易维护的优雅 Go 代码体系。
17
2026.02.27
本专题聚焦 Golang 的测试与调试体系,系统讲解单元测试、表驱动测试、基准测试与覆盖率分析方法,并深入剖析调试工具与常见问题定位思路。通过实践示例,引导建立可验证、可回归的工程习惯,从而持续提升代码可靠性与可维护性。
2
2026.02.27
AO3官网最新入口合集,汇总2026年可用官方及镜像链接,助你快速稳定访问Archive of Our Own平台。阅读专题下面的文章了解更多详细内容。
208
2026.02.27
热门下载
相关下载
精品课程


