Home>

This article shares to you the second part of the homemade php framework,Including the use of models and databases,Here are shown to you through specific examples,Hope everyone likes it

What is a model?

Our web system will definitely deal with all kinds of data,During actual development,Often a class corresponds to one or more data tables of a relational database.Two problems arise here.

1. Classes and data tables,Modification by one party results in modification by the other,As long as the data table structure is uncertain,The development of business logic can hardly start

2. Obtaining data will involve the concatenation of many SQL statements,If the data structure changes,These sql need to be rewritten

If i want to develop a blogging system,We first design two models and two data tables

The first data sheet,The table name is post, which stores blog posts,Data are as follows:

Chapter 2 Data Sheet,The table name is comment, which stores comments for blog posts,Data are as follows:

Post and comment are one-to-many relationships,Each blog post corresponds to multiple comments,Each comment belongs to only one article.

Before the design of the model class,We first define three interfaces

interface imodel {
public static function all ();
public static function get ($id);
public static function where ($condition, $value);
}

Defining the model class

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

These three interfaces are responsible for three kinds of queries:traversal queries,Conditional query,Query by number,In fact, the design of these three interfaces is not the most scientific.Even the get method is just a special form of where,But this design does not affect our engineering,It even helps to understand,We will make changes to this code later.

The reason why the SQL splicing is completed in the model class is that it is not necessary to repeatedly write SQL in the subclass.

Then the definition of the post class

class postmodel extends model {
public $postid;
public function __construct () {
parent ::__ construct ();
parent ::$table="post";
}
}

And the definition of the comment class

class commentmodel extends model {
public $commentid;
public function __construct () {
parent ::__ construct ();
parent ::$table="comment";
}
}

We can write such code in the method of the controller to complete the call data

$post=new postmodel ();
$post ::all ();
$arr=$post ::get ("1");
var_dump ($arr);
$comment=new commentmodel ();
$arr=$comment ::get ("2");
var_dump ($arr);

we discover,This code is very concise,But problems followed.When we query SQL,There are also many complex concatenated queries such as join operations, so splicing SQL is still inevitableThis complicated question,We'll fix it later.

Models and databases

First write a db abstract class, specifying the methods that the class needs to implement

abstract class db {
private $ip;
private $user;
private $pwd;
private $name;
private $connection;
abstract public function execute ($sql);
abstract public function query ($sql);
}

Here take mysql data as an example,Of course, you can also implement a set of sqlite database interfaces.

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

Speaking of database classes,The above writing is still not the best,Because we can use the singleton pattern to ensure that the db class is only initialized once,To save the overhead of hardware resources,But this is not the subject of this section,Let's talk about design patterns later.

php
  • Previous Detailed steps to install Scrapy under CentOS7
  • Next Detailed explanation of the basic principles of Android custom controls (1)