Backend Development
PHP Tutorial
Build your own PHP framework - define the ORM interface and build the PHP framework orm_PHP tutorial
Build your own PHP framework - define the ORM interface and build the PHP framework orm_PHP tutorial
Build your own PHP framework - define the ORM interface and build the PHP framework orm
In the previous blog, we abstracted the base class of Controller and implemented the page Functions that render and return JSON strings.
As a framework, what are we missing now? Yes, everyone should have noticed that we have never connected to the database before, and we lack an ORM (Object Relational Mapping).
There are three ways to connect to MySQL in PHP, namely using native functions, mysqli extension and PDO extension. For details, you can check out my previous blog "Learning PHP - Three Ways to Connect to MySQL".
Which one should we choose? Considering that as a framework, we cannot support only one database, we chose 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, even these databases can be connected using PDO, but in some specific cases, there are still some differences. For details, please refer to the PDO documentation
Since my computer only has mysql installed now, the subsequent code will only test the mysql database and not other databases.
First we will put these contents in the src/db folder. We need to define the interface. Here we will install the simplest one first.
What do we need to achieve? The simplest is the addition, deletion, modification and query of data.
Suppose we now have an article table and a corresponding Model Article. How do we want to use it?
<span>//</span><span> 选出id为1的一篇文章</span> <span>$article</span> = Article::findOne(['id' => 1<span>]); </span><span>//</span><span> 选出status是unpublished的所有文章</span> <span>$articles</span> = Article::findAll(['status' => 'unpublished'<span>]); </span><span>//</span><span> 将id为1的所有文章的status更新为published</span> Article::updateAll(['id' => 2], ['status' => 'published'<span>]); </span><span>//</span><span> 删除所有id为1的文章</span> Article::deleteAll(['id' => 2<span>]); </span><span>//</span><span> $article是之前选出的id为1的文章 // 更新它的属性status为unpublished</span> <span>$article</span>->status = 'unpublished'<span>; </span><span>//</span><span> 保存更新到数据库</span> <span>$article</span>-><span>update(); </span><span>//</span><span> 删除该文章</span> <span>$article</span>-><span>delete(); </span><span>//</span><span> 创建一个新文章</span> <span>$article</span> = <span>new</span><span> Article(); </span><span>$article</span>->name = 'My first article'<span>; </span><span>$article</span>->status = 'published'<span>; </span><span>//</span><span> 将该文章保存到数据库中</span> <span>$article</span>->insert();
Probably listed above are the uses after our simple ORM implementation. Based on this, we can define the following interface:
<?<span>php
namespace sf\db;
</span><span>interface</span><span> ModelInterface
{
</span><span>public</span> <span>static</span> <span>function</span><span> tableName();
</span><span>public</span> <span>static</span> <span>function</span><span> primaryKey();
</span><span>public</span> <span>static</span> <span>function</span> findOne(<span>$condition</span><span>);
</span><span>public</span> <span>static</span> <span>function</span> findAll(<span>$condition</span><span>);
</span><span>public</span> <span>static</span> <span>function</span> updateAll(<span>$condition</span>, <span>$attributes</span><span>);
</span><span>public</span> <span>static</span> <span>function</span> deleteAll(<span>$condition</span><span>);
</span><span>public</span> <span>function</span><span> insert();
</span><span>public</span> <span>function</span><span> update();
</span><span>public</span> <span>function</span><span> delete();
}</span>
This file is placed in the src/db folder. This is the simplest interface I can think of so far. There may be some omissions. We will continue to improve it during development. For the time being we will implement this first.
This is an interface. Later we will have a BaseModel class to implement this interface, and then all Models will inherit BaseModel to implement it.
Okay, let’s stop here today. Project content and blog content will also be put on Github, and everyone is welcome to make suggestions.
code: https://github.com/CraryPrimitiveMan/simple-framework/tree/0.4
blog project: https://github.com/CraryPrimitiveMan/create-your-own-php-framework
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20445
7
13592
4
What are the PHP open source MVC frameworks?
Aug 23, 2023 pm 01:26 PM
PHP open source mvc frameworks include Laravel, Symfony, CodeIgniter, Yii and Phalcon, etc. Detailed introduction: 1. Laravel is a popular PHP framework. It provides simple and elegant syntax and rich functions. It has a powerful routing system, database abstraction layer, queue processing, cache management and authentication functions. Laravel also provides An active community and extensive documentation resources make learning and using easier; 2. Symfony and more.
What are the mvc frameworks for php
Jul 24, 2023 am 10:52 AM
PHP's mvc frameworks include: 1. Laravel, which has a simple, elegant and scalable syntax, providing rich functions and powerful development tools; 2. Symfony, known for its flexibility and scalability, provides many components and Tools; 3. CodeIgniter, a simple and fast MVC framework with clear and concise code and lightweight size, suitable for rapid development of small and medium-sized Web applications; 4. Yii, a high-performance MVC framework that focuses on security performance and scalability, etc.
Detailed explanation of MVC framework development in Go language
Jun 03, 2023 am 10:02 AM
With the development of Internet technology and the trend of globalization, more and more developers choose to use Go language for development, and the MVC framework is a widely used Web framework. This article will introduce in detail the development of the MVC framework in the Go language, aiming to help developers better understand and use the MVC framework. 1. Introduction to MVC framework MVC (Model-View-Controller) is an architectural pattern in software development. It divides an application into three core parts: Model and View.
What are the MVC frameworks in PHP?
May 12, 2023 pm 09:40 PM
With the development of Internet technology, the MVC framework has become the most popular idea and model in Web development. Among them, PHP language, as a Web development language, also has a rich MVC framework. This article will introduce some commonly used PHPMVC frameworks. 1. Laravel Laravel is currently one of the most popular MVC frameworks in PHP and an open source PHPWeb framework created by Taylor Otwell. Laravel adopts modern PH
What are php mvc
Aug 01, 2023 pm 05:29 PM
PHP mvc includes Laravel, Symfony, CodeIgniter and Yii. 1. Laravel, which provides a wealth of functions and tools for quickly developing efficient web applications; 2. Symfony, which provides reusable components and modules; 3. CodeIgniter, which provides simple and powerful development tools and functions; 4 , Yii, provides rich functions and flexible scalability.
What are the mvc frameworks for php?
Aug 02, 2023 pm 01:31 PM
PHP's mvc frameworks include: 1. Laravel, a powerful MVC framework with an active community that provides a large number of documents and tutorials; 2. Symfony, a stable and powerful MVC framework that provides highly customizable components and Bundle concepts; 3. CodeIgniter, a simple and flexible MVC framework with small size and fast execution speed; 4. Yii, a high-performance MVC framework that provides rich features; 5. Phalcon, a high-performance MVC framework; 6. CakePHP, etc. .
What are the mvc frameworks in php
Aug 23, 2023 am 11:25 AM
MVC frameworks in PHP include Laravel, Symfony, CodeIgniter, Yii, Phalcon, CakePHP and Zend Framework, etc. Detailed introduction: 1. Laravel is one of the most popular PHP frameworks at present. It provides many useful functions and tools, such as routing, ORM, database migration, template engine, etc. Laravel has concise syntax and elegant design, making development People can quickly build high-performance web applications and more.
What are the MVC frameworks in PHP7.0?
May 27, 2023 pm 04:51 PM
What are the MVC frameworks in PHP7.0? With the rapid development of Internet applications, more and more websites and enterprise applications choose to use the PHP programming language to develop, and the MVC (Model-View-Controller) architecture has become a commonly used architectural pattern in PHP development. The basic idea of MVC is to divide the application into three modules: Model, View and Controller to improve the maintainability and scalability of the program. In PHP7.





