thinkscape / activerecord
适用于PHP 5.4+的现代ActiveRecord实现
Requires
- php: >=5.4.3
Requires (Dev)
- doctrine/dbal: 2.3.*
- phpunit/phpunit: ~3.7
- satooshi/php-coveralls: ~0.6
- zendframework/zend-db: 2.2.*
Suggests
- doctrine/dbal: Doctrine DBAL integration.
- pecl-mongo: MongoDB integration
- zendframework/zend-db: Zend\Db integration.
This package is not auto-updated.
Last update: 2024-09-23 15:10:22 UTC
README
适用于PHP 5.4+的现代ActiveRecord实现
它易于使用,易于维护,并且在使用与设计良好的用户代码结合时性能出色。ActiveRecord是一个架构模式,用于向域对象添加数据库CRUD功能。
安装
要求
- PHP 5.4.0或更高版本
- 使用以下之一进行数据库连接
使用Composer安装
- 在您的应用程序目录中运行
composer require thinkscape/activerecord:dev-master
- 确保您正在使用Composer自动加载器:
include "vendor/autoload.php";
- 按照快速入门说明进行操作。
手动安装
- 通过以下方式获取源代码:
- 通过以下方式设置类自动加载:
- 使用提供的自动加载器:
require "init_autoload.php";
,或者 - 将
src
目录添加为命名空间Thinkscape\ActiveRecord
到您现有的自动加载器。
- 按照快速入门说明进行操作。
在快速入门之前,请确保您正在使用PHP 5.4,您已将组件安装到您的应用程序中,并且您已包含Composer自动加载器或包含的autoload_register.php。
与Zend Framework 2一起使用
- 使用上述方法之一安装源代码。
- 在您的
config/application.config.php
中启用TsActiveRecord
模块。 - 将
docs/activerecord.global.php.dist
复制到您的应用程序目录中的config/autoload/activerecord.global.php
。 - 编辑
config/autoload/activerecord.global.php
并分配默认数据库适配器。 - 有关使用ActiveRecord与ZF2的更多信息。
与Symfony 2一起使用
- 有关使用ActiveRecord与Symfony 2的更多信息。
文档
快速入门
1) 使您的类成为ActiveRecord
ActiveRecord用于向现有的模型类添加数据库功能。让我们创建一个简单的ActiveRecord类。
use Thinkscape\ActiveRecord; class Country { use ActiveRecord\Core; use ActiveRecord\Persistence\ZendDb; protected static $_dbTable = 'countries'; protected static $_properties = [ 'name', 'continent', 'population' ]; }
2) 连接到数据库
所有持久化方法(如ZendDb、DoctrineDBAL等)都需要一个有效的数据库连接。我们必须创建一个新的连接适配器,并使用ActiveRecord对其进行配置
use Zend\Db\Adapter\Adapter; // Create Zend\Db MySQLi adapter $adapter = new Adapter(array( 'driver' => 'Mysqli', 'database' => 'my_application', 'username' => 'developer', 'password' => 'developer-password' )); // Method 1. Set default adapter for all ActiveRecord instances Thinkscape\ActiveRecord\Persistence\ZendDb::setDefaultDb($adapter); // Method 2. Set default adapter for Country class Country::setDefaultDb($adapter); // Method 3. Create an instance and assign an adapter to it $finland = new Country(); $finland->setDb($adapter);
3) 插入、更新和删除记录
// Create new record $finland = new Country(); $finland->setName('Finland'); $finland->save(); // INSERT INTO country (name) VALUES ("Finland") // Update $finland->setName('Maamme'); $finland->save(); // UPDATE country SET name = "Maamme" // Delete $finland->delete(); // DELETE FROM country WHERE id = 1
4) 从数据库检索记录
$first = Country::findFirst(); // SELECT * FROM country ORDER BY id ASC LIMIT 1 $countryById = Country::findById(220); // SELECT * FROM country WHERE id = 220 $countryByName = Country::findOneBy('name', 'Finland'); // SELECT * FROM country WHERE name = "Finland" LIMIT 1 $countryByName = Country::findOne([ 'name' => 'Finland' ]); // SELECT * FROM country WHERE name = "Finland" LIMIT 1 $allEuropeanCountries = Country::findAll([ 'continent' => 'Europe' ]); // SELECT * FROM country WHERE continent = "Finland" $allBigCountries = Country::findAll([ ['population', 'gt', 30000000] ]); // SELECT * FROM country WHERE population >= 30000000
5) 为您的类添加更多功能
-
ActiveRecord\AttributeMethods
-
ActiveRecord\Aliasing
-
ActiveRecord\Aggregations
-
ActiveRecord\Associations
-
ActiveRecord\Conversion
-
ActiveRecord\CounterCache
-
ActiveRecord\Callbacks
-
ActiveRecord\Inheritance
-
ActiveRecord\Integration
-
ActiveRecord\Locking\Optimistic
-
ActiveRecord\Locking\Pessimistic
-
ActiveRecord\ModelSchema
-
ActiveRecord\NestedAttributes
-
ActiveRecord\Reflection
-
ActiveRecord\Readonly
-
ActiveRecord\ReadonlyAttributes
-
ActiveRecord\Scoping
-
ActiveRecord\Serialization
-
ActiveRecord\Timestamp
-
ActiveRecord\Transactions
-
ActiveRecord\Validations