jaapoost / 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: 3.7.*
- dev-master
- 0.0.1
- dev-php72
- dev-php71
- dev-v.0.0.1
- dev-model-touch
- dev-fetch-cms
- dev-add-on-validations
- dev-multiple_model_dirs
- dev-psr-0-compliant-test
- dev-association-method
- dev-gh324-callback-not-triggered
- dev-gh-issue-291
- dev-identity-map
- dev-fix-line-endings
- dev-datetime-format
- dev-gh-issue-298
- dev-gh-296
- dev-psr-0-compliant
- dev-colored-readme
- dev-informative-exception
- dev-quoted_validations
- dev-namespace_precedence
This package is not auto-updated.
Last update: 2024-09-10 18:23:54 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_directories(array(
'/path/to/your/model_directory',
'/some/other/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 将默认使用您的开发数据库。对于测试或生产,您只需根据当前环境('测试'或'生产')设置默认连接
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'
贡献
有关如何为 PHP ActiveRecord 贡献信息的说明,请参阅 CONTRIBUTING.md。