mobixcode / php-activerecord
php-activerecord 是一个基于 ActiveRecord 模式的开源 ORM 库。
Requires
- php: >=5.3.0
Requires (Dev)
- pear/log: ~1.12
- pear/pear_exception: 1.0-beta1
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-09-22 03:07:16 UTC
README
作者
- @kla - Kien La
- @jpfuentes2 - Jacques Fuentes
- 以及这些优秀的贡献者
http://www.phpactiverecord.org/
简介
ActiveRecord 的简要概述
Active record 是一种访问数据库数据的方法。一个数据库表或视图被封装成一个类,因此一个对象实例与表中的一行绑定。对象创建后,在保存时会在表中添加新行。任何加载的对象都从数据库中获取其信息;当一个对象更新时,表中的对应行也会更新。封装类为表或视图中的每一列实现访问器方法或属性。
更多详细信息请见 这里。
这个实现受到了 Ruby on Rails ActiveRecord 的启发,因此大量借鉴了其实现。我们在尽量保持其约定俗成的同时,由于方便或必要性而有所偏离。当然,对于熟悉 rails 的用户来说,会有一些明显的区别。
最低要求
- PHP 5.3+
- 相应数据库的 PDO 驱动
支持的数据库
- MySQL
- SQLite
- PostgreSQL
- Oracle
特性
- 查找方法
- 动态查找方法
- 写入方法
- 关系
- 验证
- 回调
- 序列化(json/xml)
- 事务
- 支持多适配器
- 一些杂项选项,例如:别名/受保护的/可访问的属性
安装
设置非常简单直接。你主要需要关注三个配置点
- 设置模型自动加载目录。
- 配置你的数据库连接。
- 设置用于你的环境的数据库连接。
示例
ActiveRecord\Config::initialize(function($cfg) { $cfg->set_model_directory('/path/to/your/model_directory'); $cfg->set_connections( array( 'development' => 'mysql://username:password@localhost/development_database_name', 'test' => 'mysql://username:password@localhost/test_database_name', 'production' => 'mysql://username:password@localhost/production_database_name' ) ); });
或者(不使用 5.3 闭包)
$cfg = ActiveRecord\Config::instance(); $cfg->set_model_directory('/path/to/your/model_directory'); $cfg->set_connections( array( 'development' => 'mysql://username:password@localhost/development_database_name', 'test' => 'mysql://username:password@localhost/test_database_name', 'production' => 'mysql://username:password@localhost/production_database_name' ) );
PHP ActiveRecord 默认使用你的开发数据库。对于测试或生产,你只需根据当前环境('test' 或 'production')设置默认连接
ActiveRecord\Config::initialize(function($cfg) { $cfg->set_default_connection(your_environment); });
一旦你配置了这三个设置,就完成了。ActiveRecord 会为你处理其余部分。它不需要你将表模式映射到 yaml/xml 文件。它会查询数据库以获取这些信息,并将其缓存起来,以便不会为单个模式进行多次数据库调用。
基本 CRUD 操作
检索
这些是你查找和检索数据库记录的基本方法。更多详细信息请参阅 查找器 部分。
$post = Post::find(1); echo $post->title; # 'My first blog post!!' echo $post->author_id; # 5 # also the same since it is the first record in the db $post = Post::first(); # finding using dynamic finders $post = Post::find_by_name('The Decider'); $post = Post::find_by_name_and_id('The Bridge Builder',100); $post = Post::find_by_name_or_id('The Bridge Builder',100); # finding using a conditions array $posts = Post::find('all',array('conditions' => array('name=? or id > ?','The Bridge Builder',100)));
创建
这里我们通过实例化一个新对象并调用 save() 方法来创建一个新的帖子。
$post = new Post(); $post->title = 'My first blog post!!'; $post->author_id = 5; $post->save(); # INSERT INTO `posts` (title,author_id) VALUES('My first blog post!!', 5)
更新
要更新,你只需先找到一个记录,然后更改其属性之一。它保留了一个“脏”属性数组(已被修改的属性),因此我们的 SQL 只会更新修改过的字段。
$post = Post::find(1); echo $post->title; # 'My first blog post!!' $post->title = 'Some real title'; $post->save(); # UPDATE `posts` SET title='Some real title' WHERE id=1 $post->title = 'New real title'; $post->author_id = 1; $post->save(); # UPDATE `posts` SET title='New real title', author_id=1 WHERE id=1
删除
删除记录不会 销毁 对象。这意味着它会调用 SQL 删除数据库中的记录,但你仍然可以使用该对象。
$post = Post::find(1); $post->delete(); # DELETE FROM `posts` WHERE id=1 echo $post->title; # 'New real title'
贡献
请参阅CONTRIBUTING.md以了解如何为PHP ActiveRecord做出贡献。