Build your own PHP Framework-define the ORM interface

Source: Internet
Author: User
Tags php framework
: This article mainly introduces how to build your own PHP framework-the interface for defining ORM. For more information about PHP tutorials, see. In the previous blog, we abstracted the Controller base class to implement page rendering and return the JSON string function.

What else do we lack as a framework? Yes, you should have noticed that we have never connected to a database before, and we lack an ORM (Object Relational Mapping ).

There are three methods to connect to mysql in php: Native function, mysqli extension, and PDO extension, for details, refer to my previous blog "PHP learning-three ways to connect to MySQL".

Which one should we choose? Considering that a framework does not support only one type of database, 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, even if these databases can all be connected and used using PDO, there are still some differences in some specific cases. for details, refer to the PDO documentation.

Since I only installed mysql on my computer, the subsequent code will only test the mysql database, and will not test other databases.

First, we will put the content in the src/db folder. we need to define the interface. here we will first install the simplest one.

What do we need to implement? The simplest is to add, delete, modify, and query data.

Suppose we have an article table, a corresponding Model Article. how do we use it?

// Select an article with id 1 $ Article = article: findOne (['id' => 1]); // select all articles whose status is unpublished $ articles = Article: findAll (['status' => 'unpublished']); // update the status of all articles whose id is 1 to publishedArticle: updateAll (['id' => 2], ['status' => 'hhed']); // delete all articles with id 1 Article: deleteAll (['id' => 2]); // $ article is the previously selected article with id 1 // update its attribute status to unpublished $ article-> status = 'unpublished '; // Save the update to the database $ article-> update (); // delete this article $ article-> delete (); // create a new article $ Article = new article (); $ article-> name = 'My first article'; $ article-> status = 'published '; // save the article to the database $ article-> insert ();

The following is an example of how to use the simple ORM after implementation. Based on this, we can define the following interface:

 

This file is placed in the src/db folder. this is the simplest interface that I have come up with and may be omitted. we will continue to improve it during development. We will follow this implementation for the moment.

This is an interface. then we will have a BaseModel class to implement this interface, and all models will inherit the BaseModel for implementation.

Now, we will be here today. The project content and blog content will also be put on Github. You are welcome to give suggestions.

Code: https://github.com/CraryPrimitiveMan/simple-framework/tree/0.4

Blog project: https://github.com/CraryPrimitiveMan/create-your-own-php-framework

The above describes how to build your own PHP Framework-define the ORM interface, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.