「七天自制PHP框架」第二天:模型与数据库

往期回顾:「七天自制PHP框架」第一天:路由与控制器,点击此处

什么是模型?

我们的WEB系统一定会和各种数据打交道,实际开发过程中,往往一个类对应了关系数据库的一张或多张数据表,这里就会出现两个问题。

1.类和数据表,一方修改会导致另一方的修改,只要数据表结构不定下来,业务逻辑的开发几乎没法开工

2.获取数据时会牵涉很多SQL语句的拼接,如果数据结构变动,这些SQL需要改写

假如要开发一个博客系统,我们先设计两个Model和两张数据表

第一张数据表,表名是post,存储了博客文章,数据如下:

第二章数据表,表名是comment,存储了博客文章的评论,数据如下:

post和comment是一对多的关系,每一篇博客文章对应了多条评论,每一条评论只属于一篇文章。

Model类的设计之前,我们先定义好三个接口

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

这三个接口分别负责了三种查询:遍历查询,条件查询,按编号查询,其实这三种接口的设计并不是最科学的,甚至get方法不过是where的一种特殊形式,但是这样的设计并不影响我们工程,甚至也有助于理解,我们后期会对这段代码做改动。

之所以在Model类里就完成了SQL的拼接,就是希望在子类中不必重复再写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' ;
     }
}

我们可以在控制器的方法中写这样的代码来完成调用数据

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 );

我们发现,这样的代码很简洁,但是问题也随之而来,我们SQL查询时候,还有很多复杂的联表查询如join操作,如此,拼接SQL还是不可避免的,这个复杂的问题,我们放在后面解决。

模型与数据库

先写一个DB抽象类,规定类需要实现的方法

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数据为例,当然你也完全可以实现一套Sqlite数据库的接口。

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

谈到数据库类,上述的写法仍不是最好的,因为我们可以使用单例模式来保证DB类只有一次初始化,来节省硬件资源的开销,但这不是本节的主题,我们把设计模式放在之后来谈。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值