适合mongoDB和PHP 5.3+的数据映射器

1.0.5 2013-11-21 01:20 UTC

README

Moa是一个对象-文档映射器,旨在在mongoDB中持久化业务域对象。

Build Status

为什么选择Moa?

  • 只做最基本的工作以确保其有用性
  • Mongo查询方法已经很强大,所缺少的只是验证和类型化的反序列化
  • 旨在轻松集成到现有应用程序中
  • 无依赖
  • 经过良好测试
  • 轻松创建具有反序列化行为和验证规则的定制类型

安装

入门

在你的应用程序启动代码中,添加类似以下内容

<?php

$connection = new Mongo(); // or whatever
Moa::setup($connection->mydatabase);

可选地,可以提供一个回调来按需懒加载连接

<?php

Moa::setup(function() {
    $connection = new Mongo();
    return $connection->someDb; 
});

还可以配置额外的数据库

<?php

$connection = new Mongo(); // or whatever
Moa::setup($connection->mydatabase);
Moa::instance()->addDatabase('anotherDb', $connection->differentDb); // also takes a callback

定义模型

模型类可以这样定义

<?php

class TestDocument extends Moa\DomainObject
{
    public function properties()
    {
        return array(
            'myInt' => new Moa\Types\IntegerField(array('required' => true)),            
            'myString' => new Moa\Types\StringField(),
            'myArray' => new Moa\Types\ArrayField(),
            'myOwnSelf' => new Moa\Types\EmbeddedDocumentField(array('type'=>'TestDocument')),
        );
    }
}
  • 有关字段类型及其行为的完整列表,请参阅Moa\Types命名空间
  • 还可以定义索引(覆盖DomainObject::indexes()
  • 域对象还可以指定它们希望持久化的数据库(覆盖DomainObject::getDatabaseName()

查询

查询语法与默认PHP mongo驱动程序相同,但可以从您希望查询的域对象静态访问,例如

<?php

$docs = TestDocument::find(array('myString'=>'value'));

// it is also possible to use cursor methods
$docs = TestDocument::find(array('myString'=>'value'))->skip(20)->limit(10);

// findOne also works
$doc = TestDocument::findOne(array('myString'=>'value')); // this could except

// Documents may be saved via a call to save()
$doc->myInt = 123;
$doc->save();

// Documents can be deleted
TestDocument::remove(array('myString' => 'value')); // Deletes all documents with a field 'myString' with value of 'value'