Models and databases of self-made PHP frameworks and self-made php frameworks

Source: Internet
Author: User

Models and databases of self-made PHP frameworks and self-made php frameworks

What is a model?

Our WEB system will certainly deal with various types of data. In actual development, a class usually corresponds to one or more data tables of a relational database. Here there will be two problems.

1. Modifications to classes and data tables may lead to modifications from the other party. As long as the data table structure is not fixed, development of business logic is almost impossible.

2. Data acquisition involves splicing of many SQL statements. If the data structure changes, these SQL statements need to be rewritten.

To develop a blog system, we first design two models and two data tables.

In the first data table, the table name is post, which stores blog articles. The data is as follows:

Chapter 2 data table with the table name "comment", stores the comments of blog articles. The data is as follows:

The relationship between post and comment is one-to-many. Each blog post corresponds to multiple comments, and each comment belongs to only one article.

Before designing the Model class, we define three interfaces.

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

Define 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 types of queries: traversal query, conditional query, and serial number query. In fact, these three interfaces are not the most scientific design, even the get method is a special form of where, but such a design does not affect our project, and even helps to understand it. We will modify this code later.

The reason why SQL is spliced in the Model class is that you do not need to write SQL again in the subclass.

Then the Post class definition

class PostModel extends Model{public $postid;public function __construct(){parent::__construct();parent::$table='post';}}

Definition of Comment class

class CommentModel extends Model{public $commentid;public function __construct(){parent::__construct();parent::$table='comment';}}

We can write such code in the Controller Method 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 found that such code is concise, but the problem also arises. During SQL queries, there are still many complicated join table queries, such as join operations. As a result, splicing SQL is inevitable, we will solve this complicated problem later.

Models and databases

First, write a DB abstract class that specifies the implementation method of the class.

abstract class DB{private $IP;private $user;private $pwd;private $name;private $connection;abstract public function Execute($sql);abstract public function Query($sql);}

Here we 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 the database class, the above writing method is still not the best, because we can use the singleton mode to ensure that the DB class is only initialized once, to save the hardware resource overhead, but this is not the topic of this section. We will discuss the design pattern later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.