MicroactiverecordlibraryinPHP (an AR library implemented by micro PHP) an AR library implemented by micro PHP
Small size with detailed comments only 400 rows in total
Support Chain call
Supported Links
Preface
When I first came into contact with ActiveRecord and was learning Yii, I thought it was really convenient to use AR to operate the database. As a result, when I switched to other frameworks, I felt that I could not operate the database without AR !!! Especially when I use something that is not easy to say about the framework in the middle to write micro-PHP sites, I feel that handwriting SQL is too ugly.
Later, I also came into contact with some excellent independent ORM libraries, such as my favorite idiorm. I even came up with the idea of using Yii AR separately! But it was still not implemented...
In the past 13 years, I had been idle for some time, so I wanted to implement a ActiveRecord class myself. I planned to train my hands and use it later.
Preparation
Before getting started, I searched for a lot of php AR libraries or ORM libraries on github and stackoverflow.
Https://github.com/j4mie/idiorm
Https://github.com/vrana/notorm
Https://github.com/PrimalPHP/Record
Https://github.com/spadgos/Record
Https://github.com/sgoen/php.pdo.orm
Https://github.com/jaceju/example-my-orm
Of course, the best of them is idiorm and notorm. there are 1600 + and 600 + stars on github respectively.
For idiorm, I personally like the interfaces and usage provided by idiorm. In addition, the entire library is a single file, although the file contains nearly 2500 lines. But it is already a small library. Of course, the file is a little long and looks a little effort-consuming.
Relatively speaking, notorm is designed more OO. However, the NotORM_Result class clearly shows a large string concatenation SQL method, which still makes people think it is not very good.
Design
After reading a bunch of such ORM libraries, I am very disgusted with the method of directly splicing SQL, but this is an unavoidable problem. at last, I always need to generate an SQL string.
The key is to find a comparison"Elegance.
Expression
One day when I was looking at the spliced SQL, I accidentally felt that the query conditions behind the where clause in the SQL statement were always one by one expressions (one operand plus one operator plus one operand, for example: name = 'demo' and email = 'Demo @ demo.com ').
The structure of this expression looks like (1 + 1) * (2 + 2) in mathematics.
Even "and" or "is an operator. In addition to binary expressions, there is also a mona1 expression (an expression with only one operand, similar to-1 in mathematics ). In this way, the keywords such as select, from, where, delete, and update in SQL can be regarded as "yes"Operator.
Then a simple SQL statement can be divided into one expression:
select email, password from `user` where email = 'demo@demo.com';
SelectEmail, password
FromUser
WhereEmail = 'Demo @ demo.com'
Email='Demo @ demo.com'
The preceding SQL statements split up a total of four expressions. The where part is a nested expression.
Implement Expressions
Therefore, I specifically defined an Expressions class to represent each part of SQL. This class only has one _ toString method to generate an SQL statement as needed.
class Expressions extends Base { public function __toString() { return $this->source. ' '. $this->operator. ' '. $this->target; }}
Of course, there is a special expression "()", which is special in that operators are separated.
Interface
When the expression class is extracted, when ActiveRecord is used to query the database, every verb entered is simply an expression inserted in a proper place. Only at the end will the _ toString method of each expression be called at a time to combine SQL statements. for the sake of security, the PDO binding parameter form is used to prevent the risk of SQL injection!
For example:
$ User-> equal ('id', 1); only generates a "id =: ph1" expression in the where clause, and saves an array (': ph1' => 1. The abbreviated "eq" and "ne", "ge" interfaces are also provided. There are also interface functions such as "select", "from", "group", "order", "limit", "top", and "where.
Magic
To reduce the amount of code, the _ call magic method is used here to avoid defining a method for each action.
Link
In addition to the basic SQL operations, the ActiveRecord method similar to Yii is also implemented so that the Relation method can simply query data through the primary/foreign key relationship.
Three relationships are defined: "HAS_ONE", "has_detail", and "BELONGS_TO.
After defining the relationship, you can obtain the associated data by simply accessing the attribute.
Project
Project address: https://github.com/lloydzhou/activerecord
Document address: https://lloydzhou.github.io/activerecord
Submitted to packagist.org. you can install it through composer.
composer require lloydzhou/activerecord
DEMO
To improve the database while testing. Therefore, I used this library to write a simple blog with another Router and MicroTpl, which basically covered the APIs of these databases.