aaroncox/epicmongo

dev-master 2015-09-24 23:09 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:57:41 UTC


README

PHP的MongoDb ORM (受Shanty Mongo启发,但无需Zend Framework即可重新构建)

此库尚不完整,以下列出的许多功能仍在开发中。

特性

简单查询

通过集合的短名称快速查询集合,返回Document或DocumentSet。

<?php
$result = Epic_Mongo::db('shortname')->findOne();	// Returns Document
$results = Epic_Mongo::db('shortname')->find();	// Returns DocumentSet
?>

创建文档类型

创建具有特定要求、功能和可扩展性的不同文档类型。

<?php
// A 'user' that someone would be logged in as
class LIB_Mongo_Document_User extends Epic_Mongo_Document {
	// The collection the documents are saved into
	protected static $_collectionName = 'users';
}

// A 'schema' is created for the connection to MongoDb
class LIB_Mongo_Schema extends Epic_Mongo_Schema {
	protected $_db = 'test_database';	// Defines which Database we use
	protected $_typeMap = array(
		'user' => 'LIB_Mongo_Document_User'	// This maps the 'shortname' of 'user' to the class 'LIB_Mongo_Document_User'
	);
}
?>

轻松创建文档

轻松创建一个正确类型的新文档。

<?php 
// Create a Sample User 
$user = Epic_Mongo::doc('new:user');	// Adding 'new:' and then the 'shortname' from the schema
$user->id = 1;
$user->username = 'admin';
$user->password = 'password';
$user->save();
?>

文档字段要求

为文档类型中的特定字段创建要求。以下是一些不同选项的示例

  • 'doc:Class_Name': (可选) 返回时强制此字段的值为指定的Document Class。
  • 'ref:Class_Name': (可选) 返回时强制此字段的值为指定的Document Class,并自动将其转换为DBRef。
  • 'req': (可选) 保存前要求此字段必须设置。
<?php
// A 'user' that someone would be logged in as
class LIB_Mongo_Document_User extends Epic_Mongo_Document {
	// The collection the documents are saved into
	protected static $_collectionName = 'users';
}

// A 'post' that a user could create
class LIB_Mongo_Document_Post extends Epic_Mongo_Document {
	// The collection the documents are saved into
	protected static $_collectionName = 'posts';
	// Any requirements on fields for this document
	protected $_requirements = array(
		// Author is a reference to a LIB_Mongo_Document_User and is required
		'author' => array('ref:LIB_Mongo_Document_User', 'req'),	
	);
}

// The schema must contain all of the different types
class LIB_Mongo_Schema extends Epic_Mongo_Schema {
	// Which database is this schema for?
	protected $_db = 'test_database';	
	// A map of all types this schema supports
	protected $_typeMap = array(
		'user' => 'LIB_Mongo_Document_User'	// This maps the 'shortname' of 'user' to the class 'LIB_Mongo_Document_User'
		'post' => 'LIB_Mongo_Document_Post'	// This maps the 'shortname' of 'post' to the class 'LIB_Mongo_Document_Post'
	);
}

// Create a User 
$user = Epic_Mongo::doc('new:user');	// The 'shortname' from the schema
// Some random example data
$user->id = 2;
$user->username = 'author';
$user->password = 'password';
// Save the User
$user->save();

// Create a Post document for the User
$post = Epic_Mongo::doc('new:post');

// Set the User as the author of the post, no need to create a reference
$post->author = $user;

// Set Extra 'post' information
$post->id = 1;
$post->title = 'Test Post';
$post->body = 'This is a test post, posted by User #1';
$post->created = time();

// Save the Post
$post->save();
?>

引用解析

自动从DBRef引用返回适当的文档。

<?php
// This example uses the above example's classes and data
$post = Epic_Mongo::db('post')->findOne(array('id' => 1));
?> 
<!-- Renders the Post's Title -->
<h1><?= $post->title ?></h1>
<!-- Resolves the Reference for the Author, and Render's the User's Username -->
<h4><?= $post->author->username ?></h4>
<!-- Renders the Post's Body -->
<div><?= $post->body ?></div>

返回可迭代的DocumentSet

查询多个对象时,自动返回DocumentSet。

<?php
// Get all posts sorted by the time field, descending
$posts = Epic_Mongo::db('post')->find(array(), array('time' => -1))
?>
<div>
	<!-- Iterate over the Posts -->
	<? foreach($posts as $post): ?>
	<div>
		<!-- Renders the Post's Title -->
		<h1><?= $post->title ?></h1>
		<!-- Resolves the Reference for the Author, and Render's the User's Username -->
		<!-- Iteration 1 = "admin", Iteration 2 = "author" -->
		<h4><?= $post->author->username ?></h4> 
		<!-- Renders the Post's Body -->
		<div><?= $post->body ?></div>
	</div>
	<? endforeach; ?>
</div>

自动引用查询

当传入完整对象时,它将根据文档类型上的要求将其转换为引用。

<?php
// Select User #1
$user = Epic_Mongo::db('user')->findOne(array('id' => 1));
// Build a Query for the posts collection where the author is a reference of the user
$query = array(
	'author' => $user,
);
// Find all posts
$posts = Epic_Mongo::db('post')->find($query);
?>

将DocumentSet导出为数组

如果ArrayAccess和IteratorAggregate实现不足以满足您的需求,您只需要一个数组。

<?php
// Find all our posts
$posts = Epic_Mongo::db('post')->find();
echo gettype($posts); // Returns "object" (specifically Epic_Mongo_DocumentSet)
echo gettype($posts->export()); // Returns "array" 
?>

从数组创建文档

通过传递数据数组轻松填写文档。

<?php
// Build some Sample data
$values = array(
	'username' => 'admin',
	'password' => 'password',
	'email' => 'email@email.com',
);
// Pass the Array into the ->setFromArray function
$user = Epic_Mongo::doc('new:user')->setFromArray($values)->save();
echo $user->username; // returns 'admin'
echo $user->password; // returns 'password'
echo $user->email; // returns 'email@email.com'
?>