欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

自制PHP框架之模型與數(shù)據(jù)庫

 更新時間:2017年05月07日 09:33:37   作者:編程老頭  
本文給大家分享的是自制PHP框架的第二部分內(nèi)容,包括模型和數(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)文章

最新評論