mongodb/transistor
Composer 安装命令:
composer require mongodb/transistor
包简介
Simple MongoDB Object<->Document Mapper
README 文档
README
The new PHP Driver for MongoDB provides a MongoDB\BSON\Persistable interface which declares two methods to be called when storing the object, and the other when re-constructing it.
This transistor trait adds example implementation of the two methods and introduces
lightweight change tracking. This allows the object to be seamlessly updated as well.
Example classes
<?php class Person implements MongoDB\BSON\Persistable { use MongoDB\Transistor; protected $_id; protected $username; protected $email; protected $name; protected $addresses = array(); protected $_lastModified; protected $_created; } class Address implements MongoDB\BSON\Persistable { use MongoDB\Transistor; protected $_id; protected $streetAddress; protected $city; protected $postalCode; } ?>
See Person.php and Address.php for
the full implementation of these example classes -- although this is really it. No annotations or anything.
implements MongoDB\BSON\Persistable and use MongoDB\Transistor is the magic.
Simple usage
<?php /* Construct a new person */ $person = new Person("bjori", "bjori@php.net", "Hannes Magnusson"); /* Insert it */ insert($person); /* Find a person based on its username */ $person = findOne(array("username" => "bjori")); /* Get an instance of the Person object again */ var_dump($person); ?>
The above example will output something similar to
object(Person)#8 (7) {
["_id"]=>
object(MongoDB\BSON\ObjectID)#4 (1) {
["oid"]=>
string(24) "553586e2bd21b971774b7da1"
}
["username"]=>
string(5) "bjori"
["email"]=>
string(13) "bjori@php.net"
["name"]=>
string(16) "Hannes Magnusson"
["addresses"]=>
array(0) {
}
["_lastModified"]=>
NULL
["_created"]=>
object(MongoDB\BSON\UTCDatetime)#7 (0) {
}
}
Updating the object
<?php /* Continuing from previous example, $person is instanceof Person */ $person->setName("Dr. " . $person->getName()); /* Update the document */ update(array("username" => "bjori"), $person); /* Retrieve it again */ $person = findOne(array("username" => "bjori")); /* Get an instance of the Person object again */ var_dump($person); ?>
The above example will output something similar to
object(Person)#9 (7) {
["_id"]=>
object(MongoDB\BSON\ObjectID)#4 (1) {
["oid"]=>
string(24) "553586e2bd21b971774b7da1"
}
["username"]=>
string(5) "bjori"
["email"]=>
string(13) "bjori@php.net"
["name"]=>
string(16) "Dr. Hannes Magnusson"
["addresses"]=>
array(0) {
}
["_lastModified"]=>
NULL
["_created"]=>
object(MongoDB\BSON\UTCDatetime)#7 (0) {
}
}
Adding embedded objects
<?php /* Continuing from previous example, $person is instanceof Person */ /* Construct a new Address object */ $address = new Address("Manabraut 4", "Kopavogur", 200); $person->addAddress($address); /* Update the object with a new Address embedded object */ update(array("username" => "bjori"), $person); $person = findOne(array("username" => "bjori")); var_dump($person); ?>
The above example will output something similar to
object(Person)#10 (7) {
["_id"]=>
object(MongoDB\BSON\ObjectID)#4 (1) {
["oid"]=>
string(24) "553586e2bd21b971774b7da1"
}
["username"]=>
string(5) "bjori"
["email"]=>
string(13) "bjori@php.net"
["name"]=>
string(16) "Dr. Hannes Magnusson"
["addresses"]=>
array(1) {
[0]=>
object(Address)#%d (%d) {
["_id"]=>
object(MongoDB\BSON\ObjectID)#%d (%d) {
["oid"]=>
string(24) "%s"
}
["streetAddress"]=>
string(11) "Manabraut 4"
["city"]=>
string(9) "Kopavogur"
["postalCode"]=>
int(200)
["_created"]=>
object(MongoDB\BSON\UTCDatetime)#%d (0) {
}
}
}
["_lastModified"]=>
NULL
["_created"]=>
object(MongoDB\BSON\UTCDatetime)#7 (0) {
}
}
Helpers
The insert(), update() and findOne() helpers in the example above don't do anything other then wrap their respective methods on the MongoDB\Driver\Manager and setting the TypeMap, and are only there to reduce error checking needed in the examples.
Performance
Since the actual object (un-)serialization is done by the extension itself there is nothing for PHP to do -- the trait itself is under 200 lines of dead simple code.
I'm sure there are dragons, so use with care.
mongodb/transistor 适用场景与选型建议
mongodb/transistor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 157 次下载、GitHub Stars 达 28, 最近一次更新时间为 2015 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「persistence」 「mongodb」 「odm」 「driver」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mongodb/transistor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mongodb/transistor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mongodb/transistor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
This bundle provides tools to build persistence-agnostic storage layer.
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
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
统计信息
- 总下载量: 157
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 29
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD
- 更新时间: 2015-04-22