Build your own PHP framework-Define an ORM interface and build a PHP framework ORM
In the previous blog, we abstracted the base class of the controller, implemented the rendering of the page and the function of returning the JSON string.
What do we lack now as a framework? Yes, we should have noticed that we have never connected a database before, and we are missing an ORM (Object relational Mapping).
There are three ways to connect MySQL in PHP, using native functions, mysqli extensions, and PDO extensions, in detail to see my previous blog, "PHP Learning-three ways to connect to MySQL."
Which one do we choose? Given that it is not possible to support only one database as a framework, we choose to use PDO. Of course if you are sure that your framework only needs to connect to the MySQL database, you can also consider using MYSQLI.
PDO supports the following databases:
- Cubrid (PDO)
- MS SQL Server (PDO)
- Firebird (PDO)
- IBM (PDO)
- Informix (PDO)
- MySQL (PDO)
- MS SQL Server (PDO)
- Oracle (PDO)
- ODBC and DB2 (PDO)
- PostgreSQL (PDO)
- SQLite (PDO)
- 4D (PDO)
Of course, these databases can use PDO to connect to use, but in some specific cases, there is a little bit different, the details can refer to the PDO document
Since my computer now only has MySQL installed, then the code will only test the MySQL database and will not test other databases.
First we will put this content in the Src/db folder, we need to define the interface, here we will first Ann the simplest.
What do we need to achieve? The simplest is to change the data and delete the search.
Suppose we now have a article table, a corresponding model article, how do we want to use it?
//Select an article with ID 1$article= Article::findone ([' id ' = = 1]);//Choose Status is all articles of unpublished$articles= Article::findall ([' status ' = ' unpublished ')]);//Update status for all articles with ID 1 to publishedArticle::updateall ([' id ' = 2], [' Status ' = ' published ')]);//Delete all articles with ID 1Article::d eleteall ([' id ' = 2]);//$article is the previously selected article ID 1//Update its property status to unpublished$article->status = ' Unpublished ';//Save updates to database$article-update ();//Delete this article$article-Delete ();//Create a new article$article=Newarticle ();$article->name = ' My First article ';$article->status = ' published ';//Save the article to a database$article->insert ();
Probably listed above, our simple ORM implementation after the use, whereby we can define the following interface:
!--?
phpnamespace sf\db;
Interface
modelinterface{
public
static
function
TableName ();
public
static
function
primaryKey ();
public
static
function findOne (
$condition
);
public
static
function findAll (
$condition
);
public
static
function updateAll (
$condition ,
$ Attributes
);
public
static
function deleteAll (
$condition
);
public
function
Insert ();
public
function
update ();
public
function
Delete ();}
This file is placed in the src/db folder, which is the simplest interface I have ever thought of, which may be omitted, and we will continue to improve it when we develop it. For the time being we will follow this implementation first.
This is an interface, after which we will have a Basemodel class to implement this interface, and then all the model will inherit basemodel to implement.
All right, let's get here today. Project content and blog content will also be put on GitHub, welcome suggestions.
code:https://github.com/craryprimitiveman/simple-framework/tree/0.4
Blog Project:https://github.com/craryprimitiveman/create-your-own-php-framework
http://www.bkjia.com/PHPjc/1101502.html www.bkjia.com true http://www.bkjia.com/PHPjc/1101502.html techarticle Build Your own PHP framework-Define ORM interfaces, build PHP framework orm in the previous blog, we abstracted the controller's base class, implemented the rendering of the page and returned the work of the JSON string ...