自己寫的一個簡易的ORM類,給感興趣的朋友提供一點思路。
自己寫的一個簡易的ORM類,給感興趣的朋友提供一點思路。借鋻了一點TP的思路。
<?php /** * author: NickBai * createTime: 2016/11/28 0028 下午 4:00 */ class MyOrm implements ArrayAccess { public $host = '127.0.0.1'; //數據庫地址 public $dbname = 'test'; //數據庫名 public $user = 'root'; //數據庫用戶名 public $pwd = 'root'; //數據庫密碼 public $port = '3306'; //數據庫耑口 public $charset = 'utf8'; //數據庫編碼 private $conn = null; //數據庫鏈接資源 private $alias = []; //記錄全局的語句參數 private $sql; //存儲最後一條sql public function __construct() { if( is_null( $this->conn ) ){ $dsn = "mysql:host=$this->host;dbname=$this->dbname;charset=$this->charset;port=$this->port"; $this->conn = new PDO( $dsn, $this->user, $this->pwd ); } } //field語句 public function field( $field ) { if( !is_string( $field ) ){ throw new exception("field語句的參數必須爲字符串"); } $this->alias['field'] = $field; return $this; } //table語句 public function table( $table ) { if( !is_string( $table ) ){ throw new exception("table語句的參數必須爲字符串"); } $this->alias['table'] = $table; return $this; } //where語句 public function where( $where ) { $this->alias['where'] = ''; if( is_array( $where ) ){ foreach( $where as $key=>$vo ){ $this->alias['where'] .= " `$key`" . ' = ' . $vo . ' and '; } $this->alias['where'] = rtrim( $this->alias['where'], 'and ' ); }else if( is_string( $where ) ){ $this->alias['where'] = $where; }else{ throw new exception("where語句的參數必須爲數組或字符串"); } return $this; } // limit語句 public function limit( $limit ) { $this->alias['limit'] = ''; if( is_numeric( $limit ) ){ $this->alias['limit'] = '0,' . $limit; }else if( is_string( $limit ) ){ $this->alias['limit'] = $limit; }else{ throw new exception("limit語句的參數必須爲數字或字符串"); } return $this; } //order語句 public function order( $order ) { if( !is_string( $order ) ){ throw new exception("order語句的參數必須爲字符串"); } $this->alias['order'] = $order; return $this; } // group語句 public function group( $group ) { if( !is_string( $group ) ){ throw new exception("group語句的參數必須爲字符串"); } $this->alias['group'] = $group; return $this; } //解析查詢sql語句 public function ParseSelectSql() { $this->sql = 'select *'; if( !empty( $this->alias['field'] ) ){ $this->sql = str_replace( '*', $this->alias['field'], $this->sql ); } if( empty( $this->alias['table'] ) ){ throw new exception("請用table子句設置查詢表"); }else{ $this->sql .= ' from ' . $this->alias['table']; } if( !empty( $this->alias['where'] ) ){ $this->sql .= ' where ' . $this->alias['where']; } if( !empty( $this->alias['group'] ) ){ $this->sql .= ' group by ' . $this->alias['group']; } if( !empty( $this->alias['order'] ) ){ $this->sql .= ' order by ' . $this->alias['order']; } if( !empty( $this->alias['limit'] ) ){ $this->sql .= ' limit ' . $this->alias['limit']; } } // 解析添加sql語句 public function ParseAddSql() { $this->sql = 'insert into '; if( empty( $this->alias['table'] ) ){ throw new exception("請用table子句設置添加表"); }else{ $this->sql .= $this->alias['table'] . ' set '; } return $this->sql; } // 解析更新sql語句 public function ParseUpdateSql() { $this->sql = 'update '; if( empty( $this->alias['table'] ) ){ throw new exception("請用table子句設置修改表"); }else{ $this->sql .= $this->alias['table'] . ' set '; } if( empty( $this->alias['where'] ) ){ throw new exception("更新語句必須有where子句指定條件"); } return $this->sql; } // 解析刪除sql語句 public function ParseDeleteSql() { $this->sql = 'delete from '; if( empty( $this->alias['table'] ) ){ throw new exception("請用table子句設置刪除表"); }else{ $this->sql .= $this->alias['table']; } if( empty( $this->alias['where'] ) ){ throw new exception("刪除語句必須有where子句指定條件"); } $this->sql .= ' where ' . $this->alias['where']; return $this->sql; } // 查詢語句 public function select() { $this->ParseSelectSql(); $row = $this->conn->query( $this->sql )->fetchAll( PDO::FETCH_ASSOC ); $result = []; foreach( $row as $key=>$vo ){ $arrObj = clone $this; //clone當前對象防止對this對象造成污染 $arrObj->data = $vo; $result[$key] = $arrObj; unset( $arrObj ); } return $result; } // 查詢一條 public function find() { $this->ParseSelectSql(); $row = $this->conn->query( $this->sql )->fetch( PDO::FETCH_ASSOC ); $arrObj = clone $this; //clone當前對象防止對this對象造成污染 $arrObj->data = $row; $result = $arrObj; unset( $arrObj ); return $result; } // 添加數據 public function add( $data ) { if( !is_array( $data ) ){ throw new exception("添加數據add方法參數必須爲數組"); } $this->ParseAddSql(); foreach( $data as $key=>$vo ){ $this->sql .= " `{$key}` = '" . $vo . "',"; } $this->conn->exec( rtrim( $this->sql, ',' ) ); return $this->conn->lastInsertId(); } // 更新語句 public function update( $data ) { if( !is_array( $data ) ){ throw new exception("更新數據update方法參數必須爲數組"); } $this->ParseUpdateSql(); foreach( $data as $key=>$vo ){ $this->sql .= " `{$key}` = '" . $vo . "',"; } $this->sql = rtrim( $this->sql, ',' ) . ' where ' . $this->alias['where']; return $this->conn->exec( $this->sql ); } // 刪除語句 public function delete() { $this->ParseDeleteSql(); return $this->conn->exec( $this->sql ); } // 獲取查詢數據 public function getData() { return $this->data; } // 獲取最後一次執行的sql語句 public function getLastSql() { return $this->sql; } public function __get($name) { return $this->getData()[$name]; } public function offsetExists($offset) { if( !isset( $this->getData()[$offset] ) ){ return NULL; } } public function offsetGet($offset) { return $this->getData()[$offset]; } public function offsetSet($offset, $value) { return $this->data[$offset] = $value; } public function offsetUnset($offset) { unset( $this->data[$offset] ); } }
你可以這麽用:
$orm = new MyOrm();
//查詢語句
$res = $orm->table('user')->order('id desc')->select();
$res = $orm->table('user')->where("name='test'")->order('id desc')->select();
$res = $orm->table('user')->where(['id' => 1])->order('id desc')->find();
$res = $orm->table('user')->where("age > 20")->group('group by name')->order('id desc')->limit(2)->select();
$res = $orm->table('user')->where("age > 20")->group('group by name')->order('id desc')->limit('2,2')->select();
//你可以這樣處理數據
foreach( $res as $key=>$vo ){
echo $vo->name . '<br/>';
}
//也可以這樣處理
foreach( $res as $key=>$vo ){
echo $vo['name'] . '<br/>';
}
//還可以這樣
foreach( $res as $key=>$vo ){
print_r( $vo->getData() ) . '<br/>';
}
//添加數據
$data = [
'name' => 'test1',
'age' => 20,
'password' => '21232f297a57a5a743894a0e4a801fc3',
'salt' => 'domain'
];
$res = $orm->table('user')->add( $data );
//更新數據
$res = $orm->table('user')->where(['id' => 4])->update( ['name' => 'sdfdsfdsd', 'salt' => '111'] );
//刪除數據
$res = $orm->table('user')->where(['id' => 7, 'id' => 6])->delete();
//獲取執行的sql語句
echo $orm->getLastSql();
var_dump($res);