国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > php教程 > php手冊 > 正文

一個簡易的ORM類

php中文網(wǎng)
發(fā)布: 2016-12-01 00:00:21
原創(chuàng)
1549人瀏覽過

自己寫的一個簡易的ORM類,給感興趣的朋友提供一點思路。
自己寫的一個簡易的ORM類,給感興趣的朋友提供一點思路。借鑒了一點TP的思路。 /**
?*?author:?NickBai
?*?createTime:?2016/11/28?0028?下午?4:00
?*/
class?MyOrm?implements?ArrayAccess
{
????public?$host?=?'127.0.0.1';??//數(shù)據(jù)庫地址
????public?$dbname?=?'test';???//數(shù)據(jù)庫名
????public?$user?=?'root';??//數(shù)據(jù)庫用戶名
????public?$pwd?=?'root';???//數(shù)據(jù)庫密碼
????public?$port?=?'3306';??//數(shù)據(jù)庫端口
????public?$charset?=?'utf8';???//數(shù)據(jù)庫編碼
????private?$conn?=?null;????//數(shù)據(jù)庫鏈接資源
????private?$alias?=?[];??//記錄全局的語句參數(shù)
????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語句的參數(shù)必須為字符串");
????????}

????????$this->alias['field']?=?$field;
????????return?$this;
????}

????//table語句
????public?function?table(?$table?)
????{
????????if(?!is_string(?$table?)?){
????????????throw?new?exception("table語句的參數(shù)必須為字符串");
????????}

????????$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語句的參數(shù)必須為數(shù)組或字符串");
????????}

????????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語句的參數(shù)必須為數(shù)字或字符串");
????????}

????????return?$this;
????}

????//order語句
????public?function?order(?$order?)
????{
????????if(?!is_string(?$order?)?){
????????????throw?new?exception("order語句的參數(shù)必須為字符串");
????????}

????????$this->alias['order']?=?$order;
????????return?$this;
????}

????//group語句
????public?function?group(?$group?)
????{
????????if(?!is_string(?$group?)?){
????????????throw?new?exception("group語句的參數(shù)必須為字符串");
????????}

????????$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;
????}

????//添加數(shù)據(jù)
????public?function?add(?$data?)
????{
????????if(?!is_array(?$data?)?){
????????????throw?new?exception("添加數(shù)據(jù)add方法參數(shù)必須為數(shù)組");
????????}

????????$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("更新數(shù)據(jù)update方法參數(shù)必須為數(shù)組");
????????}

????????$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?);
????}

????//獲取查詢數(shù)據(jù)
????public?function?getData()
????{
????????return?$this->data;
????}

????//獲取最后一次執(zhí)行的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();

//你可以這樣處理數(shù)據(jù)
foreach(?$res?as?$key=>$vo?){
????echo?$vo->name?.?'
';
}
//也可以這樣處理
foreach(?$res?as?$key=>$vo?){
????echo?$vo['name']?.?'
';
}
//還可以這樣
foreach(?$res?as?$key=>$vo?){
????print_r(?$vo->getData()?)?.?'
';
}

//添加數(shù)據(jù)
$data?=?[
????'name'?=>?'test1',
????'age'?=>?20,
????'password'?=>?'21232f297a57a5a743894a0e4a801fc3',
????'salt'?=>?'domain'
];
$res?=?$orm->table('user')->add(?$data?);

//更新數(shù)據(jù)
$res?=?$orm->table('user')->where(['id'?=>?4])->update(?['name'?=>?'sdfdsfdsd',?'salt'?=>?'111']?);

//刪除數(shù)據(jù)
$res?=?$orm->table('user')->where(['id'?=>?7,?'id'?=>?6])->delete();

//獲取執(zhí)行的sql語句
echo?$orm->getLastSql();

var_dump($res);

最佳 Windows 性能的頂級免費優(yōu)化軟件
最佳 Windows 性能的頂級免費優(yōu)化軟件

每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。

下載
來源:php中文網(wǎng)
本文內容由網(wǎng)友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內容,請聯(lián)系admin@php.cn
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓,幫助PHP學習者快速成長!
關注服務號 技術交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學習
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號