Home  >  Article  >  Backend Development  >  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

WBOY
WBOYOriginal
2016-07-12 08:58:441016browse

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1101502.htmlTechArticleBuild your own PHP framework - define the ORM interface and build the php framework orm. In the previous blog, we The base class of Controller is abstracted, and the function of rendering the page and returning JSON string is implemented...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn