Previous review: "Seven-day self-made PHP framework" first day: Routing and controller, click here
What is a model?
Our web system is bound to deal with a variety of data, the actual development process, often a class corresponding to a relational database of one or more tables, there will be two problems.
1. Class and data table, one party modification will cause the other party's modification, as long as the data table structure, business logic development can hardly start
2. Data acquisition involves the concatenation of many SQL statements, which need to be rewritten if the data structure changes
If we want to develop a blog system, we first design two model and two data tables
The First data table, the table name is post, stores the blog post, the data is as follows:
The second Chapter data table, the table name is comment, stores the blog post comments, the data are as follows:
Post and comment are a one-to-many relationship, and each blog post corresponds to several comments, each of which belongs to an article.
Before we design the model class, we first define three interfaces.
12345 |
interface IModel{ public static function all(); public static function get( $id ); public static function where( $condition , $value ); } |
Defining the Model class
12345678910111213141516171819202122 |
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 query, 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, and even help to understand, we will later on this code changes.
In the model class, the concatenation of SQL is done, that is, you do not want to repeat the SQL in the subclass.
Then the definition of the Post class
1234567 |
class PostModel extends Model{ public $postid ; public function __construct(){ parent::__construct(); parent:: $table = ‘post‘ ; } } |
And the definition of the comment class.
1234567 |
class CommentModel extends Model{ public $commentid ; public function __construct(){ parent::__construct(); parent:: $table = ‘comment‘ ; } } |
We can write this code in the Controller's method to complete the invocation of the data
12345678 |
$post = new PostModel(); $post ::all(); $arr = $post ::get( ‘1‘ ); var_dump( $arr ); $comment = new CommentModel(); $arr = $comment ::get( ‘2‘ ); var_dump( $arr ); |
We found that this kind of code is very concise, but the problem also comes, when we SQL query, there are many complex table query such as JOIN operation, so, splicing SQL is unavoidable, this complex problem, we put in the back to solve.
Model and Database
First, write a DB abstract class that specifies the method that the class needs to implement
1234567891011 |
abstract class db{ private $IP ; private $user ; private $pwd ; private $name ; private $connection ; abstract public function Execute ( $sql ); abstract public function Query ( $sql ); } |
Here, for example, MySQL data, of course, you can also fully implement a set of SQLite database interface.
123456789101112131415161718192021222324252627282930313233343536 |
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);
}
}
|
When it comes to database classes, the above notation is still not the best, because we can use Singleton mode to ensure that the DB class has only one initialization to save the cost of hardware resources, but this is not the topic of this section, we put the design pattern after the discussion.
"Seven-day self-made PHP framework" the next day: models and databases