自己寫的一個簡易的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
}<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.='限制'。 $this->alias['limit'];<br>
}<br>
<br>
}<br>
<br>
//解析加入sql語句<br>
公用函數 ParseAddSql()<br>
{<br>
$this->sql='插入到';<br>
if( 空($this->alias['table']) ){<br>
throw new Exception("請使用table子句設定新增表格");<br>
}其他{<br>
<br>
$this->sql .=$this->alias['table'] . ' 設定';<br>
}<br>
<br>
回$this->sql;<br>
}<br>
<br>
//解析更新sql語句<br>
公用函數 ParseUpdateSql()<br>
{<br>
$this->sql='更新';<br>
if( 空($this->alias['table']) ){<br>
throw new Exception("請使用table子句設定修改表");<br>
}其他{<br>
<br>
$this->sql .=$this->alias['table'] . ' 設定';<br>
}<br>
<br>
if( 空( $this->alias['where'] ) ){<br>
throw new exception("更新語句必須有where子句指定條件");<br>
}<br>
<br>
回$this->sql;<br>
}<br>
<br>
//解析刪除sql語句<br>
公用函數 ParseDeleteSql()<br>
{<br>
$this->sql = '刪除自';<br>
if( 空($this->alias['table']) ){<br>
throw new Exception("請使用table子句設定刪除表");<br>
}其他{<br>
<br>
$this->sql .= $this->alias['table'];<br>
}<br>
<br>
if( 空( $this->alias['where'] ) ){<br>
throw new exception("刪除語句必須有where子句指定條件");<br>
}<br>
<br>
$this->sql.='其中'. $this->alias['where'];<br>
<br>
回$this->sql;<br>
}<br>
<br>
<br>
//查詢語句<br>
公用函數 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>
公用函數 getData()<br>
{<br>
返回 $this->data;<br>
}<br>
<br>
//取得最後執行的sql語句<br>
公用函數 getLastSql()<br>
{<br>
回$this->sql;<br>
}<br>
<br>
公用函數__get($name)<br>
{<br>
返回 $this->getData()[$name];<br>
}<br>
<br>
公用函數 offsetExists($offset)<br>
{<br>
if(!isset($this->getData()[$offset]) ){<br>
則返回 NULL;<br>
}<br>
}<br>
<br>
公用函數 offsetGet($offset)<br>
{<br>
返回 $this->getData()[$offset];<br>
}<br>
<br>
公用函數 offsetSet($offset, $value)<br>
{<br>
返回 $this->data[$offset] = $value;<br>
}<br>
<br>
公用函數 offsetUnset($offset)<br>
{<br>
取消設定($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')->order('id desc' )->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')->order('id desc' )->group('group by name')->order('id desc')-> limit('2,2')->select();<br>
<br>
//你可以這樣處理資料<br>
foreach($res as $key=>$vo){<br>
回顯$vo->名稱。 '<br>';<br>
}<br>
//也可以這樣處理<br>
foreach($res as $key=>$vo){<br>
回顯$vo['name']。 '<br>';<br>
}<br>
//還可以這樣<br>
foreach($res as $key=>$vo){<br>
print_r($vo->getData()). '<br>';<br>
}<br>
<br>
//新增資料<br>
$數據 = [<br>
'姓名' => '測試1',<br>
'年齡' => 20、<br>
'密碼' => '21232f297a57a5a743894a0e4a801fc3',<br>
'鹽' => ‘網域名稱’<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);