一个简易的ORM类
自己写的一个简易的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);

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

主板上SPDIFOUT连接线序最近我遇到了一个问题,就是关于电线的接线顺序。我上网查了一下,有些资料说1、2、4对应的是out、+5V、接地;而另一些资料则说1、2、4对应的是out、接地、+5V。最好的办法是查看你的主板说明书,如果找不到说明书,你可以使用万用表进行测量。首先找到接地,然后就可以确定其他的接线顺序了。主板vdg怎么接线连接主板的VDG接线时,您需要将VGA连接线的一端插入显示器的VGA接口,另一端插入电脑的显卡VGA接口。请注意,不要将其插入主板的VGA接口。完成连接后,您可以

本文推荐全球十大数字货币交易APP,涵盖币安(Binance)、OKX、火币(Huobi Global)、Coinbase、Kraken、Gate.io、KuCoin、Bitfinex、Gemini和Bitstamp。这些平台在交易对数量、交易速度、安全性、合规性、用户体验等方面各有特色,例如币安以其高交易速度和广泛服务闻名,而Coinbase则更适合新手用户。选择适合自己的平台需要综合考虑自身需求和风险承受能力。 了解全球主流数字货币交易平台,助您安全高效进行数字资产交易。

本篇文章将详细介绍如何安装和注册比特币交易应用。比特币交易应用允许用户管理和交易比特币等加密货币。文章逐步指导用户完成安装和注册过程,包括下载应用程序、创建账户、进行身份验证和首次存款。文章的目标是为初学者提供清晰易懂的指南,帮助他们轻松进入比特币交易的世界。

没有单一“最好用”的数字货币交易App,选择取决于个人需求。1. OKX功能强大,币种丰富;2. Binance流动性高,交易类型多样;3. Gate.io提供质押挖矿等独特功能;4. Huobi Global界面友好,多语言支持;5. Kraken注重安全性;6. Coinbase适合新手,注重用户教育。 选择时需考虑安全性、流动性、手续费、功能、用户体验等等因素。

欧易,又称OKX,是一个全球领先的加密货币交易平台。文章提供了欧易官方安装包的下载入口,方便用户在不同设备上安装欧易客户端。该安装包支持 Windows、Mac、Android 和 iOS 系统,用户可根据自己的设备类型选择相应版本下载。安装完成后,用户即可注册或登录欧易账户,开始交易加密货币和享受平台提供的其他服务。

本文介绍了 10 个主流的加密货币交易所,涵盖了它们的成立时间、服务范围、安全性、流动性、交易费用等基本信息。这些交易所包括:OKX、Binance、Gate.io、Bitget、Coinbase、Huobi、KuCoin、Crypto.com、Gemini 和 Kraken。

本文根据全球信息,整理了全球领先的虚拟数字货币交易平台排名,包括Binance(币安)、OKX、Gate.io、Huobi(火币)、Coinbase、Kraken、Crypto.com、bitget、KuCoin和Bitstamp等。这些平台在用户数量、交易量、交易品种、安全性、合规性等方面各有特色,例如币安以其庞大的用户群和广泛的交易选择着称,Coinbase则以其在美国市场的领先地位和易用性而闻名。 选择合适的交易平台需要根据自身需求,权衡安全性、费用、交易品种等因素。

欧易交易所,原名币安,是全球领先的加密货币交易平台之一。它于2017年由赵长鹏创立,总部位于中国香港。欧易交易所为用户提供广泛的加密货币交易服务,包括现货交易、期货交易、场外交易和加密资产管理。
