定制 mongodb/transistor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mongodb/transistor

最新稳定版本:0.1.0

Composer 安装命令:

composer require mongodb/transistor

包简介

Simple MongoDB Object<->Document Mapper

README 文档

README

Build Status

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.

统计信息

  • 总下载量: 157
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: BSD
  • 更新时间: 2015-04-22