自制PHP框架之模型與數(shù)據(jù)庫
什么是模型?
我們的WEB系統(tǒng)一定會和各種數(shù)據(jù)打交道,實際開發(fā)過程中,往往一個類對應(yīng)了關(guān)系數(shù)據(jù)庫的一張或多張數(shù)據(jù)表,這里就會出現(xiàn)兩個問題。
1.類和數(shù)據(jù)表,一方修改會導(dǎo)致另一方的修改,只要數(shù)據(jù)表結(jié)構(gòu)不定下來,業(yè)務(wù)邏輯的開發(fā)幾乎沒法開工
2.獲取數(shù)據(jù)時會牽涉很多SQL語句的拼接,如果數(shù)據(jù)結(jié)構(gòu)變動,這些SQL需要改寫
假如要開發(fā)一個博客系統(tǒng),我們先設(shè)計兩個Model和兩張數(shù)據(jù)表
第一張數(shù)據(jù)表,表名是post,存儲了博客文章,數(shù)據(jù)如下:

第二章數(shù)據(jù)表,表名是comment,存儲了博客文章的評論,數(shù)據(jù)如下:

post和comment是一對多的關(guān)系,每一篇博客文章對應(yīng)了多條評論,每一條評論只屬于一篇文章。
Model類的設(shè)計之前,我們先定義好三個接口
interface IModel{
public static function all();
public static function get($id);
public static function where($condition,$value);
}
定義Model類
class Model implements IModel{
public static $table;
public static $db;
public function __construct(){
self::$db=new MySQL();
}
public static function get($id){
return self::where('id',$id);
}
public static function where($condition,$value){
$sql=sprintf("select * from %s where %s='%s'",self::$table,$condition,$value);
return self::$db->Query($sql);
}
public static function all(){
$sql=sprintf("select * from %s",self::$table);
return self::$db->Query($sql);
}
}
這三個接口分別負責(zé)了三種查詢:遍歷查詢,條件查詢,按編號查詢,其實這三種接口的設(shè)計并不是最科學(xué)的,甚至get方法不過是where的一種特殊形式,但是這樣的設(shè)計并不影響我們工程,甚至也有助于理解,我們后期會對這段代碼做改動。
之所以在Model類里就完成了SQL的拼接,就是希望在子類中不必重復(fù)再寫SQL。
然后是Post類的定義
class PostModel extends Model{
public $postid;
public function __construct(){
parent::__construct();
parent::$table='post';
}
}
還有Comment類的定義
class CommentModel extends Model{
public $commentid;
public function __construct(){
parent::__construct();
parent::$table='comment';
}
}
我們可以在控制器的方法中寫這樣的代碼來完成調(diào)用數(shù)據(jù)
$post=new PostModel();
$post::all();
$arr=$post::get('1');
var_dump($arr);
$comment=new CommentModel();
$arr=$comment::get('2');
var_dump($arr);
我們發(fā)現(xiàn),這樣的代碼很簡潔,但是問題也隨之而來,我們SQL查詢時候,還有很多復(fù)雜的聯(lián)表查詢?nèi)鏹oin操作,如此,拼接SQL還是不可避免的,這個復(fù)雜的問題,我們放在后面解決。
模型與數(shù)據(jù)庫
先寫一個DB抽象類,規(guī)定類需要實現(xiàn)的方法
abstract class DB{
private $IP;
private $user;
private $pwd;
private $name;
private $connection;
abstract public function Execute($sql);
abstract public function Query($sql);
}
這里以MySQL數(shù)據(jù)為例,當(dāng)然你也完全可以實現(xiàn)一套Sqlite數(shù)據(jù)庫的接口。
class MySQL extends DB{
public function MySQL(){
/*Config*/
$this->IP='*';
$this->ServerID='*';
$this->ServerPassword='*';
$this->DataBaseName='*';
/*End of Config*/
$this->connection=mysqli_connect($this->IP,$this->ServerID,$this->ServerPassword,$this->DataBaseName);
if(!$this->connection){
die('Could not connect'.$this->connection);
}
mysqli_query($this->connection,'set names utf8');
}
public function Execute($sql){
return mysqli_query($this->connection,$sql);
}
public function Query($sql){
$result=mysqli_query($this->connection,$sql);
$arr=array();
while($row=mysqli_fetch_array($result)){
$arr[]=$row;
}
return $arr;
}
public function Close(){
mysqli_close($this->connection);
}
}
談到數(shù)據(jù)庫類,上述的寫法仍不是最好的,因為我們可以使用單例模式來保證DB類只有一次初始化,來節(jié)省硬件資源的開銷,但這不是本節(jié)的主題,我們把設(shè)計模式放在之后來談?!?/p>
相關(guān)文章
PHP數(shù)學(xué)運算函數(shù)大匯總(經(jīng)典值得收藏)
這篇文章主要介紹了PHP數(shù)學(xué)運算函數(shù),匯總分析了常見的PHP數(shù)學(xué)運算函數(shù)的功能,使用方法與注意事項,需要的朋友可以參考下2016-04-04
PHP實現(xiàn)微信公眾號企業(yè)號自定義菜單接口示例
這篇文章主要介紹了PHP實現(xiàn)微信公眾號企業(yè)號自定義菜單接口,結(jié)合實例形式分析了php自定義微信菜單類及微信接口相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
PHP中round()函數(shù)對浮點數(shù)進行四舍五入的方法
這篇文章主要介紹了PHP中round()函數(shù)對浮點數(shù)進行四舍五入的方法,通過詳盡的實例對round()函數(shù)的各種常見用法進行了歸納整理,是非常實用的技巧,需要的朋友可以參考下2014-11-11
探討Smarty中如何獲取數(shù)組的長度以及smarty調(diào)用php函數(shù)的詳解
本篇文章是對Smarty中如何獲取數(shù)組的長度以及smarty調(diào)用php函數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06
php simplexmlElement操作xml的命名空間實現(xiàn)代碼
這是今天中午發(fā)生的事情,有人在群里求助,比如xml中如果標(biāo)記是<xx:xxxx>content</xx:xxxx>這樣的情況下,取不到 xx:xxxx 為下標(biāo)的值。2011-01-01

