Build your own PHP framework-define the ORM interface and build the php framework orm

Source: Internet
Author: User
Tags findone

Build your own PHP framework-define the ORM interface and build the php framework orm

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:

<?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. 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

 

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.