sailing-goat/php-activerecord-future

php-activerecord 是一个基于 ActiveRecord 模式的开源 ORM 库。

v1.2.3 2020-02-14 16:54 UTC

This package is auto-updated.

Last update: 2024-09-15 06:55:33 UTC


README

Build Status

  • 最初由 @kla - Kien La 创建
  • @jpfuentes2 - Jacques Fuentes 分支并维护
  • 为 PHP 7.x 的未来使用分支并维护

此分支的目的是维护 1.2.0 版本,以适应未来的 PHP 7.x 兼容性。代码库的更新将版本为 1.2.x。

简介

ActiveRecord 的简要概述

Active record 是一种访问数据库数据的方法。数据库表或视图被包装成一个类,因此对象实例与表中的一行绑定。在创建对象后,在保存时会在表中添加新行。任何加载的对象都会从数据库中获取其信息;当一个对象被更新时,表中的相应行也会被更新。包装类为表或视图中的每个列实现了访问器方法或属性。

更多详细信息请参见 此处

此实现受到 Ruby on Rails ActiveRecord 的启发,因此大量借鉴了其约定。我们尽量保持了这些约定,但主要由于方便或必要而有所偏离。当然,如果有用户熟悉 rails,将会明显看到一些差异。

最低要求

  • PHP 7.0+
  • 您数据库的 PDO 驱动程序

支持的数据库

  • MySQL
  • SQLite
  • PostgreSQL
  • Oracle

功能

  • 查找方法
  • 动态查找方法
  • 写入方法
  • 关系
  • 验证
  • 回调
  • 序列化(json/xml)
  • 事务
  • 支持多个适配器
  • 一些其他选项,例如:别名/受保护/可访问属性

安装

设置非常简单直观。基本上只有三个配置点您需要关注

  1. 设置模型自动加载目录。
  2. 配置您的数据库连接。
  3. 设置用于您的环境的数据库连接。

示例

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'
     )
   );
});

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做出贡献。