parm / parm
PHP Active Record for MySQL。连接到您的数据库并生成对象关系映射。内置数据库查询处理器,用于自定义查询。能够使用闭包处理行
Requires
- php: >=5.5.0
- doctrine/dbal: ^2.5
- mustache/mustache: ^2.3
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ^4.7.7
- ramsey/uuid: ^3.0.0
- dev-master
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.10
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1
- dev-feature/3.0
- dev-develop
This package is auto-updated.
Last update: 2023-06-07 04:06:02 UTC
README
Parm

PHP Active Record for MySQL -- PHP, AR, ORM, DAO, OMG!
它根据您的架构生成模型,其强大的基于闭包的查询处理能力和处理大数据集的能力使其强大而灵活。
- 遵循PSR-4规范,并与Composer兼容
- 处理所有CRUD(创建、读取、更新和删除)
- 轻松以JSON格式输出数据供API使用
- 快速查询,可以轻松限制为表中的字段子集("select first_name, last_name from table" 与 "select * from table" 对比)。当使用字段子集时,您仍然可以使用对象。
- SQL UPDATE操作最小化,并且只发送到数据库的修改列/字段
- 基于闭包的查询处理,让您以高效和完全自定义的方式处理数据
- PagedCollection使得逐页遍历记录集变得非常容易(一次处理10,000条记录中的1,000条)
- 模型可以生成到命名空间或全局命名空间中
- 在保存到数据库时处理所有输入值的转义
- 绑定自动转义查询值
- 使用基于闭包的过程模型处理任何SQL查询(多表和连接),并将结果轻松输出到数组或JSON
- 您可以轻松扩展工厂和对象来封装模型的逻辑(胖模型)
- 将返回字段的正确数据类型(如果它是MySQL int(11)列,将返回整数)
- 方法链,如过滤器、限制等
- 如果您没有将它们生成到PSR自动加载目录中,将生成所有生成类/模型的自动加载器
- 使用MySQL时区表转换时区(如果已加载时区表)
- 使用Mustache模板创建生成的代码
- 使用PHPUnit和Travis CI的完整测试套件
- 完全文档化,使用PHPDoc "DocBlock"注释生成的类,以帮助您的IDE
示例用法
下面有更多详细示例。注意:您还应该查看测试,因为它们包含更多示例
$user = User::findId(17); // find record with primary key 17
$user->setFirstName("John"); // set the first name
$user->save(); // save to the database
设置和生成
Composer (Packagist)
https://packagist.org.cn/packages/parm/parm
"parm/parm": "^3.0"
示例数据库配置
\Parm\Config::setupConnection('parm_namespaced_tests', 'database-name-on-server','database-host','database-username','database-password');
或者你可以传递一个 Doctrine DBAL 连接
\Parm\Config::addConnection('parm-global-tests', new Doctrine\DBAL\Connection([
'dbname' => $GLOBALS['db__name'],
'user' => $GLOBALS['db_username'],
'password' => $GLOBALS['db_password'],
'host' => $GLOBALS['db_host']
], new Doctrine\DBAL\Driver\PDOMySql\Driver(), null, null));
示例生成器配置
$generator = new Parm\Generator\DatabaseGenerator(Parm\Config::getDatabase('people-database-name'));
$generator->setDestinationDirectory('/classes/dao/peopleDatabase');
$generator->setGeneratedNamespace("\\Dao\\PeopleDatabase");
$generator->generate();
扩展模型
你可以轻松扩展模型以封装简单的业务逻辑。以下示例为了简洁使用了这些扩展对象。
class User extends Project\Dao\UserDaoObject
{
static function getFactory(\Doctrine\DBAL\Connection $connection = null)
{
return new UserFactory($connection);
}
//example function
public function getFullName()
{
return $this->getFirstName() . " " . $this->getLastName();
}
}
class UserFactory extends Project\Dao\UserDaoFactory
{
function loadDataObject(Array $row = null)
{
return new User($row);
}
}
CRUD
创建
$user = new User();
$user->setFirstName('Ada');
$user->setLastName('Lovelace');
$user->setEmail('lovelace@example.com');
$user->save();
echo $user->getId() // will print the new primary key
读取
查找 id 为 17 的对象。
// shorthand
$user = User::findId(17);
// you can also use a factory
$f = new UserFactory();
$user = $f->findId(17);
从表中查找所有对象(返回一个集合)
$f = new UserFactory();
$users = $f->findAll();
限制查询为前 20 行
$f = new UserFactory();
$f->setLimit(20);
$users = $f->getCollection();
根据列过滤对象(以下四个语句都是等价的)
$f = new UserFactory();
$f->whereEquals("archived","0");
$users = $f->getCollection();
$f = new UserFactory();
$f->whereEquals(User::ARCHIVED_COLUMN,"0");
$f = new UserFactory();
$f->addBinding(new new \Parm\Binding\EqualsBinding(User::ARCHIVED_COLUMN,"0"));
// if use_global_namespace.php is included
$f = new UserFactory();
$f->addBinding(new EqualsBinding(User::ARCHIVED_COLUMN,"0"));
包含搜索对象
// looking for users with example.com in their email
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\ContainsBinding("email","example.com"));
// looking for users with example.com in their email using a case sensitive search
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\CaseSensitiveContainsBinding("email","example.com"));
基于字符串的 WHERE 子句
// looking for active users
$f = new UserFactory();
$f->addBinding("user.archived != 1");
基于数组过滤
// looking for users created before today
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\InBinding("zipcode_id",array(1,2,3,4)));
使用对象根据外键过滤
$f = new UserFactory();
$company = Company::findId(1);
$f->addBinding(new \Parm\Binding\ForeignKeyObjectBinding($company));
基于日期的搜索
// looking for users created before today
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\DateBinding("create_date",'<',new \DateTime()));
更新
更新是最小化的,只为改变的字段创建 UPDATE 语句。如果名字在改变,这个例子将生成 "UPDATE user SET first_name = 'John' WHERE user_id = 17;"
$user = User::findId(17);
$user->setFirstName("John");
$user->save();
删除
删除单个记录。
$user = User::findId(18);
$user->delete();
删除多个记录。
// delete all archived users
$f = new UserFactory();
$f->addBinding(new EqualsBinding("archived","1"));
$f->delete();
函数(计数、求和等)
执行计数查询
$f = new UserFactory();
$f->addArchivedFalseBinding()
$count = $f->count(); // count of all not archived users
执行求和查询
$f = new UserFactory();
$total = $f->sum("salary"); // count of all not archived users
转换为 JSON
$user->toJSON() // a json ready Array()
$user->toJSONString() // a json string { 'id' : 1, 'firstName' : 'John', 'lastName' : 'Doe', ... }
闭包
使用闭包(匿名函数)处理查询到的每一行。
$f = new UserFactory();
$f->process(function($user)
{
if(!validate_email($user->getEmail()))
{
$user->setEmail('');
$user->save();
}
});
数据处理程序
数据处理程序非常适合使用闭包在完全自定义的 SELECT 查询中处理结果。
缓冲查询以提高速度
$p = new DatabaseProcessor('example');
$p->setSQL('select first_name, last_name from user');
$p->process(function($row)
{
echo $row['first_name'];
print_r($row);
});
性能
限制从数据库检索的字段。你仍然可以使用对象
$f = new UserFactory();
$f->setSelectFields("first_name","last_name","email");
$users = $f->query();
获取一个准备好的 JSON 数组
$f = new UserFactory();
$f->setSelectFields("first_name","last_name","email");
$userJSON = $f->getJSON(); // returns an an array of PHP objects that can be easily encoded to [ { 'id' : 1, 'firstName' : 'John', 'lastName' : 'Doe', 'email' : 'doe@example.com'}, ... ]
其他有趣的功能
灵活的查询
用于编写自定义 WHERE 子句的查找方法(返回对象)
$f = new UserFactory();
$users = $f->findObjectWhere("where archived != 1 and email like '%@example.com'");
转换时区
注意:需要安装 mysql 数据库中的时区
$dp = new DatabaseProcessor('database');
$centralTime = $dp->convertTimezone('2012-02-23 04:10PM', 'US/Eastern', 'US/Central');
要求
- PHP 5.4 或更高版本
- MySQL