Micro ActiveRecord Library in PHP (a miniature PHP-implemented AR Library)

Source: Internet
Author: User
A miniature PHP implementation of the AR Library

The size is very small, with a detailed note, only 400 lines in total.
Supports chained calls
Support relationships


The first contact with ActiveRecord is in the learning of Yii, at that time, I think it is really convenient to use AR operation database. So that later turned to some other frameworks, feel no AR seems to be unable to manipulate the database!!! In particular, in the middle of their own use some simple framework of things to write micro-PHP site, the feeling of handwritten SQL is too ugly.
I have also come into contact with some of the best independent ORM libraries, like my favorite idiorm. I even had the idea of trying to get the ar out of yii alone! But then it didn't come true ...
In 13, there was a period of time to work too leisurely, so at that time I want to achieve a ActiveRecord class, one intends to practice practiced hand, and to also intend to use their own later on.

Get ready

Before you start writing, look for a whole bunch 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, with the 1600+ and 600+ star on GitHub's doorstep.
    For Idiorm, the individual is very fond of the interface it provides and how it is used. And the entire library is a single file, although the file is nearly 2500 lines. But it's already a small library. Of course the file is a bit long and looks a bit laborious.
    Relatively notorm designed to be more oo. But in the Notorm_result this class obviously saw a large string of strings splicing the way of SQL, or it makes people feel not very good.


    After looking at a bunch of these ORM libraries, I was very disgusted with the way the SQL was directly stitched up, but this was an issue that could not be avoided, and it was always necessary to generate a SQL string.
    The key is to find a more " elegant " way to complete the work of the stitching.

    An expression

    One day when I look at the stitching of SQL, I occasionally feel that the query condition behind the where in SQL is always an expression (an operand plus an operator plus an operand, for example: Name= ' demo ' and Email= ' demo@demo.com ').

    The structure of this expression seems to be similar to the one in mathematics.

    Even And,or is a one-operator. Refine it again. In addition to the two-tuple expression, there is a unary expression (an expression of only one operand, similar to the 1 in mathematics). In this case, the SQL inside the select,from,where,delete,update such keywords can be counted as " operator ".

    Then a simple SQL can be decomposed into one expression:

    Select email, password from ' user ' where email = ' demo@demo.com ';
  • Select Email, password

  • from user

  • where email = ' demo@demo.com '

  • Email = ' demo@demo.com '

  • The above SQL altogether splits out 4 expressions, where the where part is a nested expression.



    So, I specifically defined a expressions class to represent the various parts of SQL. This class has only one __tostring method that generates SQL when it is last needed

    Class Expressions extends Base {public    function __tostring () {        return $this->source. ' '. $this->operator. ' '. $this->target;    }}

    Of course, there is a more special expression is "()", the expression is special in the operator is separate.


    When a class of expressions is extracted, when querying a database using ActiveRecord, each verb is inserted into an expression only where appropriate. Only in the last time will call each expression of the __tostring method combination of SQL, for security purposes, using the form of PDO binding parameters to prevent the risk of SQL injection!
    Like what:

    $user->equal (' id ', 1); just a "id=:p H1" expression is generated in the WHERE clause, and an array (':p H1 ' =>1) is saved as an argument. The same kind of interface functions as "eq" and "ne" and "GE" are also provided. There are also interface functions such as "select", "from", "group", "Order", "Limit", "Top" and "where".

    Magic method

    To reduce the amount of code, the __call magic method is used here to avoid defining methods for every action.


    In addition to the basic operation of SQL, but also implemented a similar Yii ActiveRecord method allows the relation method can be simply through the primary foreign key relationship query data.
    The three relationships of "Has_one", "Has_many" and "belongs_to" are defined.
    The associated data can be obtained through a simple access property after the relationship is defined.


    Project Address: Https://github.com/lloydzhou/activerecord
    Document Address: Https://lloydzhou.github.io/activerecord
    has been submitted to packagist.org can be installed via composer

    Composer require Lloydzhou/activerecord


    To improve the library while testing. So using this library in conjunction with another router and Microtpl wrote a simple blog that basically covers the API of these libraries.

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