Step-by-step writing of PHP's Framework (16)

Source: Internet
Author: User
Tags define array commit functions connect sql php database query

I split the model into pieces, a,b,c,d,e,f, what do these pieces represent?

First A, this base represents the base class of the entire frame, and all model files must inherit from the base class;

Since B's content depends on D, let's say first D,d has a connectionmanager, which is responsible for managing the connection and shutdown of the database and database-driven choices, the name itself does not feel good, first will be, since it is responsible for the database connection and shutdown, It is certain that it has the following methods:

1 <?php
2 public class ConnectionManager {
3 public static function getconnection () {}
4 public static function Releaseconnection () {}
5 }

Since the database connection is shared for all models, we set the Connnectionmanager to a single example.

D in the right there is pdo,mysql_*, what does this mean, in fact, is to represent a variety of drivers, you may be using the more advanced PDO, you may be using the older mysql_* this, it is possible that you are using Mysqli,ado and so on, It is because of this confusion in the PHP database that we need to define a contract in the framework in which all the driver classes are written to implement, so that regardless of the underlying implementation of the driver class, the content seen in the upper layer is consistent.

Now we define the contract:

01 <?php
02 Interface Idbdriver {
03 function execute ($sql);
04 function connect ();
05 function Close ();
06 function Getallbyobject ();
07 function Getallbyassocarray ();
08 function Getallbyarray ();
09 function BeginTrans ();
10 function commit ();
11 function rollback ();
12 }

Connect is the connection to the database, execute is to execute a SQL, if this SQL is a query, then getallbyobject represents the result of the query returned through the object, Getallbyassocarray represents the result in the associative array returned, Getallbyarray represents the return of the result in an ordinary array, the BeginTrans represents the opening of a transaction, that is, opening the autocommit,commit is committing the transaction, rollback is rolling the transaction back, and close is shutting down db.

In fact, when the result of the query is returned, it can also be encapsulated as a iterator.

Okay, now let's take a look at B, first look at Modelbase, which is actually provided to the table model and the relational model, which encapsulates various externally visible functions, such as query,insert,update,delete,execute,fetchall and so on, That means that all of the driver classes in B are not directly accessible, then who accesses, is modelbase, but it is not directly accessed, but through the ConnectionManager to access.

Because ConnectionManager will automatically select the driver according to the profile, so Modelbase doesn't know exactly which driver class it is calling, but since the driver class follows the contract, Modelbase can invoke the method above the contract without having to control what the driver is.

01 <?php
02 Class Modelbase extends Base {
03 protected $_db = null;
04 Public Function __construct () {
05 $this->_db = Connectionmanager::getconnection ();
06 }
07 Public function Execute ($sql) {
08 $this->_db->execute ($sql);
09 }
10 }

For example, the above code, first in the instantiation of the Modelbase, will call the ConnectionManager getconnection get the database connection, of course, because ConnectionManager is a single example, So even if 10 modelbase classes are instantiated at the same time, the database connection operation is only once.

With the above contract, executing a SQL at this level is very simple, as long as the Execute method of the driver class is invoked, that is, the $this->_db->execute ($sql);

Said Modelbase, say again SQL Parse, its main function is to complete SQL parsing, when need SQL parsing, realize the database ORM time.

This layer is also called by Modelbase, assuming that there is now a query operation outside, the way to Invoke is $this->where ()->order ()->select (), of course, the parameters of each function I did not write, this function chain is very friendly, SQL Parse will parse the actual meaning of these functions, then return to Modelbase,modelbase to get the parsed SQL, and then execute $this->_db->execute ($sql), so whatever the pattern is, Ultimately, the Execute method of the driver class is invoked to execute a single SQL.

The following c is actually easy to understand, is the table model and relational model, because some operations have been done in the modelbase, so long as the table model and relational model to inherit modelbase. Table model because it is dealing with a single table of content, so it has additional content, such as in the execution of a $this->select (), we do not specify the content of the query, the system should be able to identify the table to query what it is, may ultimately parse the SQL is: SELECT * FROM XXX, the relationship model is the same.

I'm done. C,e is also very easy to understand, the model layer is actually called the Relational model or table model to achieve specific business.

F is processing and ordinary database other than the content, such as cache, because here is only to talk about the idea, so this piece of content is temporarily not write, if there is time to speak later, if not, that even.



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.