什么是模型?
我們的web系統(tǒng)一定會(huì)和各種數(shù)據(jù)打交道,實(shí)際開發(fā)過程中,往往一個(gè)類對(duì)應(yīng)了關(guān)系數(shù)據(jù)庫的一張或多張數(shù)據(jù)表,這里就會(huì)出現(xiàn)兩個(gè)問題。
1.類和數(shù)據(jù)表,一方修改會(huì)導(dǎo)致另一方的修改,只要數(shù)據(jù)表結(jié)構(gòu)不定下來,業(yè)務(wù)邏輯的開發(fā)幾乎沒法開工
2.獲取數(shù)據(jù)時(shí)會(huì)牽涉很多sql語句的拼接,如果數(shù)據(jù)結(jié)構(gòu)變動(dòng),這些sql需要改寫
假如要開發(fā)一個(gè)博客系統(tǒng),我們先設(shè)計(jì)兩個(gè)model和兩張數(shù)據(jù)表
第一張數(shù)據(jù)表,表名是post,存儲(chǔ)了博客文章,數(shù)據(jù)如下:
第二章數(shù)據(jù)表,表名是comment,存儲(chǔ)了博客文章的評(píng)論,數(shù)據(jù)如下:
post和comment是一對(duì)多的關(guān)系,每一篇博客文章對(duì)應(yīng)了多條評(píng)論,每一條評(píng)論只屬于一篇文章。
model類的設(shè)計(jì)之前,我們先定義好三個(gè)接口
1
2
3
4
5
|
interface imodel{ public static function all(); public static function get( $id ); public static function where( $condition , $value ); } |
定義model類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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 ); } } |
這三個(gè)接口分別負(fù)責(zé)了三種查詢:遍歷查詢,條件查詢,按編號(hào)查詢,其實(shí)這三種接口的設(shè)計(jì)并不是最科學(xué)的,甚至get方法不過是where的一種特殊形式,但是這樣的設(shè)計(jì)并不影響我們工程,甚至也有助于理解,我們后期會(huì)對(duì)這段代碼做改動(dòng)。
之所以在model類里就完成了sql的拼接,就是希望在子類中不必重復(fù)再寫sql。
然后是post類的定義
1
2
3
4
5
6
7
|
class postmodel extends model{ public $postid ; public function __construct(){ parent::__construct(); parent:: $table = 'post' ; } } |
還有comment類的定義
1
2
3
4
5
6
7
|
class commentmodel extends model{ public $commentid ; public function __construct(){ parent::__construct(); parent:: $table = 'comment' ; } } |
我們可以在控制器的方法中寫這樣的代碼來完成調(diào)用數(shù)據(jù)
1
2
3
4
5
6
7
8
|
$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查詢時(shí)候,還有很多復(fù)雜的聯(lián)表查詢?nèi)鏹oin操作,如此,拼接sql還是不可避免的,這個(gè)復(fù)雜的問題,我們放在后面解決。
模型與數(shù)據(jù)庫
先寫一個(gè)db抽象類,規(guī)定類需要實(shí)現(xiàn)的方法
1
2
3
4
5
6
7
8
9
10
11
|
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)然你也完全可以實(shí)現(xiàn)一套sqlite數(shù)據(jù)庫的接口。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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ù)庫類,上述的寫法仍不是最好的,因?yàn)槲覀兛梢允褂脝卫J絹肀WCdb類只有一次初始化,來節(jié)省硬件資源的開銷,但這不是本節(jié)的主題,我們把設(shè)計(jì)模式放在之后來談。
原文鏈接:http://www.cnblogs.com/sweng/p/6624845.html