Table of Contents
前言
准备
设计
表达式
实现
Expressions
接口
魔术方法
关系
项目
DEMO
Home Backend Development PHP Tutorial micro activerecord library in PHP(一个微型的PHP实现的AR库)

micro activerecord library in PHP(一个微型的PHP实现的AR库)

Jun 23, 2016 pm 01:25 PM

一个微型的PHP实现的AR库

体积很小带详尽的注释总共只有400行
支持链式调用
支持关系

前言

最开始接触ActiveRecord是在学习Yii的时候,那个时候觉得用AR操作数据库真的是太方便了。以至于后来转向其他的一些框架的时候,感觉没有了AR似乎就不能操作数据库了一样!!!特别是中间自己使用一些简单的说不上框架的东西来写微型的PHP站点的时候,感觉手写SQL太难看。
后来也接触过一些优秀的独立的ORM的库,比如我最喜欢的idiorm. 甚至曾经萌生了想要把Yii的AR单独拿出来用的想法!但是后来还是没有实现。。。
在13年的时候,有一段时间上班时间太清闲,所以当时就想自己实现一个ActiveRecord类,一来打算练练手,而来也打算自己以后用的上。

准备

在开始动手写之前,在github,以及stackoverflow上面寻找了一大堆的php的AR库或者ORM的库。

  • 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

  • 当然其中最好的就是idiorm以及notorm,在github上门分别有1600+和600+的star。
    拿idiorm来说,个人非常喜欢它提供的接口以及使用方式。而且整个库也就单个文件,虽然文件将近2500行。但是已经算是一个小型的库了。当然文件有点长,看起来有点费劲。
    相对而言notorm设计得更加OO一些。不过在其中的NotORM_Result这个class中明显看到一大片字符串拼接SQL的方式,还是让人觉得看起来不是很好。

    设计

    在看了一堆这样的ORM的库之后,非常反感那种直接拼接SQL的方式,但是这又是一个无法避开的问题,最后总是需要生成一个SQL字符串的。
    关键在于需要找出一种比较”优雅“的方式来完成这个拼接的工作。

    表达式

    有一天在看拼接的SQL的时候偶然间感觉SQL里面where后面的那些查询条件总是一个一个的表达式(一个操作数加上一个操作符再加上一个操作数,例如:name='demo' and email='demo@demo.com')。

    这个表达式的结构看起来和数学里面的(1+1)*(2+2)是那么的相像。
    Copy after login

    甚至连and,or也是一个一个的操作符而已。再提炼一下除了二元表达式,还有一元表达式(只有一个操作数的表达式,类似数学里面的-1)。这样算起来SQL里面的select,from,where,delete,update这样的关键词都可以算成是”操作符“了。

    那么一个简单的SQL就可以分解成一个一个的表达式:

    select email, password from `user` where email = 'demo@demo.com';
    Copy after login
  • select email, password

  • from user

  • where email = 'demo@demo.com'

  • email = 'demo@demo.com'

  • 上面的SQL总共拆分出来了4个表达式,其中where部分是嵌套的一个表达式。

    实现

    Expressions

    所以,我专门定义了一个Expressions类用来表示SQL中的各个部分。这个类只有一个__toString方法在最后需要的时候生成SQL

    class Expressions extends Base {    public function __toString() {        return $this->source. ' '. $this->operator. ' '. $this->target;    }}
    Copy after login

    当然还有比较特殊的表达式就是"()",这个表达式特殊在于操作符是分开的。

    接口

    当提炼出来了表达式的类之后,那么在使用ActiveRecord查询数据库的时候,每输入一个动词,都只是在适当的地方插入一个表达式而已。只有在最后的时候才会一次性的调用每一个表达式的__toString方法组合SQL,为了安全考虑,使用了PDO的绑定参数的形式防止SQL注入的风险!
    比如:

    $user->equal('id', 1);只是在where子句里面生成了一个 "id=:ph1"的表达式,同时保存了一个array(':ph1'=>1)的参数而已。同样的还提供了简写的”eq“以及”ne“,”ge“等接口函数。另外还有”select“,”from“,”group“,”order“,”limit“,”top“以及”where“等接口函数。
    Copy after login

    魔术方法

    为了减少代码量,所以这里使用了__call魔术方法避免了为每一个动作都去定义个method。

    关系

    除了实现SQL的基本操作,还实现了一下类似Yii的ActiveRecord法让Relation的方法可以简单的通过主外键关系查询数据。
    定义了”HAS_ONE“,”HAS_MANY“和”BELONGS_TO“三种关系。
    可以在定义关系之后,能通过简单的的访问属性的方式获得关联的数据。

    项目

    项目地址:https://github.com/lloydzhou/activerecord
    文档地址:https://lloydzhou.github.io/activerecord
    已经提交到packagist.org可以通过composer安装

    composer require lloydzhou/activerecord
    Copy after login

    DEMO

    为了一边测试,一边完善这个库。所以使用这个库结合另外一个Router和 MicroTpl 写了一个简单的 博客 ,里面基本涵盖了这几个库的API。

    Statement of this Website
    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

    Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

    Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

    Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

    Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

    Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

    Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

    This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

    Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

    Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

    cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

    The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

    12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

    Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

    Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

    The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

    See all articles