parm/parm
最新稳定版本:4.0.5
Composer 安装命令:
composer require parm/parm
包简介
PHP Active Record for MySQL. Connects to the your database and generates the Object Relational Mapping. Built in Database Query Processor for custom queries. Ability to use closures to process rows
README 文档
README
Parm

PHP Active Record for MySQL -- PHP, AR, ORM, DAO, OMG!
It generates models based on your schema and its powerful closure based query processing and ability to handle large data sets make it powerful and flexible.
- PSR-4 Compliant and works with Composer
- Handles all the CRUD (Creating, Reading, Updating, and Deleting)
- Easily output data as JSON for APIs
- Fast queries that can easily be limited to a subset of fields in a table ("select first_name, last_name from table" vs. "select * from table"). And you can still use objects when using a subset of the fields.
- SQL UPDATEs are minimal and only the modified columns/fields are sent to the database
- Closure based query processing that lets you handle data efficiently and in a fully customizable manner
- PagedCollection makes it very easy to page through a set of records one page at a time (Go through 1,000,000 records 1,000 at a time)
- Models can be generated into a namespace or generated into the global namespace
- Handles all escaping of input values when saving to the database
- Bindings automatically escape of query values
- Process any SQL query (multiple tables and joins) using the same closure based process model. Easily output the results to an Array or JSON
- You can easily extend the Factories and Objects to encapsulate the logic of a model (fat models)
- Will return the proper data type for the field (if it is a MySQL int(11) column an integer will be returned)
- Method chaining of filters, limits, etc
- Generates an autoloader for all of the generated classes/models if you don't generate them into a PSR autoloader directory
- Convert Timezones Using MySQL Timezone Tables (if time_zone tables are loaded)
- Generated Code is creating using Mustache Templates
- Full test suite using PHPUnit and Travis CI
- Fully documented and generated classes are generated with PHPDoc "DocBlock" comments to assist your IDE
Example Usage
See much more detail examples below. Note: You should also look at the tests as they contain many more examples
$user = User::findId(17); // find record with primary key 17
$user->setFirstName("John"); // set the first name
$user->save(); // save to the database
Setup and Generation
Composer (Packagist)
https://packagist.org/packages/parm/parm
"parm/parm": "^3.0"
Example Database Configuration
\Parm\Config::setupConnection('parm_namespaced_tests', 'database-name-on-server','database-host','database-username','database-password');
or you can pass a Doctrine DBAL Connection
\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));
Example Generator Configuration
$generator = new Parm\Generator\DatabaseGenerator(Parm\Config::getDatabase('people-database-name'));
$generator->setDestinationDirectory('/classes/dao/peopleDatabase');
$generator->setGeneratedNamespace("\\Dao\\PeopleDatabase");
$generator->generate();
Extending Models
You can easily extend the models to encapsulate simple business logic. The examples below use these extended objects for brevity.
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
Creating
$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
Reading
Finding an object with id 17.
// shorthand
$user = User::findId(17);
// you can also use a factory
$f = new UserFactory();
$user = $f->findId(17);
Finding all objects form a table (returns a Collection)
$f = new UserFactory();
$users = $f->findAll();
Limit the query to the first 20 rows
$f = new UserFactory();
$f->setLimit(20);
$users = $f->getCollection();
Querying for objects filtered by a column (the following four statements are all equivalent)
$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"));
Contains searches for objects
// 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"));
String based where clauses
// looking for active users
$f = new UserFactory();
$f->addBinding("user.archived != 1");
Filter by array
// looking for users created before today
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\InBinding("zipcode_id",array(1,2,3,4)));
Filter by foreign key using an object
$f = new UserFactory();
$company = Company::findId(1);
$f->addBinding(new \Parm\Binding\ForeignKeyObjectBinding($company));
Date based searches
// looking for users created before today
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\DateBinding("create_date",'<',new \DateTime()));
Updating
Updates are minimal and create an UPDATE statement only for the fields that change. If the first name is changing this example will generate "UPDATE user SET first_name = 'John' WHERE user_id = 17;"
$user = User::findId(17);
$user->setFirstName("John");
$user->save();
Deleting
Deleting a single record.
$user = User::findId(18);
$user->delete();
Deleting multiple records.
// delete all archived users
$f = new UserFactory();
$f->addBinding(new EqualsBinding("archived","1"));
$f->delete();
Functions (Counting, Summing, etc)
Running a count query
$f = new UserFactory();
$f->addArchivedFalseBinding()
$count = $f->count(); // count of all not archived users
Running a sum query
$f = new UserFactory();
$total = $f->sum("salary"); // count of all not archived users
Convert to JSON
$user->toJSON() // a json ready Array()
$user->toJSONString() // a json string { 'id' : 1, 'firstName' : 'John', 'lastName' : 'Doe', ... }
Closures
Process each row queried with a closure(anonymous function).
$f = new UserFactory();
$f->process(function($user)
{
if(!validate_email($user->getEmail()))
{
$user->setEmail('');
$user->save();
}
});
Data Processors
Data processors are great for processing the results from an entirely custom SELECT query with closures.
Buffered Queries for Speed
$p = new DatabaseProcessor('example');
$p->setSQL('select first_name, last_name from user');
$p->process(function($row)
{
echo $row['first_name'];
print_r($row);
});
Performance
Limiting the fields that are pulled back from the database. You can still use objects
$f = new UserFactory();
$f->setSelectFields("first_name","last_name","email");
$users = $f->query();
Getting a JSON ready array
$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'}, ... ]
Other Neat Features
Flexible Queries
Find method for writing a custom where clause (returns objects)
$f = new UserFactory();
$users = $f->findObjectWhere("where archived != 1 and email like '%@example.com'");
Converting Timezones
Note: Requires time zones installed in mysql database
$dp = new DatabaseProcessor('database');
$centralTime = $dp->convertTimezone('2012-02-23 04:10PM', 'US/Eastern', 'US/Central');
Requirements
- PHP 5.4 or greater
- MySQL
parm/parm 适用场景与选型建议
parm/parm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.98k 次下载、GitHub Stars 达 57, 最近一次更新时间为 2013 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「mysql」 「Active Record」 「activerecord」 「dao」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 parm/parm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 parm/parm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 parm/parm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 4.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 58
- 点击次数: 32
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2013-05-16