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