对Shanty-Mongo使用过程的一些记录

由于工作上需要用到MongoDB,根据进度原因自己封装了一个简单的实现,当然可以完成现有的任务,在一次测试中发现异常,虽然自己通过封装其他扩展类的方法解决了问题,但是感觉到自己对MongoDB和MongoDB的php扩展了解不足,所以找了一个开源的产品Shanty-Mongo了解下其他实现方式。

这个开源库是基于ZendFramework开发的,他对php本身和其他产品也有要求:

  • PHP 5.3 or greater
  • Zend Framework 1.10.0 or greater
  • Mongodb 1.3 or greater
  • Mongo extension from pecl
满足了以上条件,你就可以开始动手了,官方说有一下特色,目前还没有完全感觉到,可能是库在无形中已经做到这些

  • ORM
  • Simple and flexible
  • Partial updates. Only changed data is sent back to the server. Also you can save or delete embeded documents individually.
  • Support for references (lazy loaded)
  • Support for inheritance
  • Optional schema enforcement: Validation and Filtering on properties
  • Embeded documents/documentsets can have custom document classes like the root document
从源码的类结构看,产品封装了MongoDBphp扩展的MongoCollection, MongoCursor,Mongo等对象,同时对MongoDB的document做了实现,提供了Document和DocumentSet的实现类;通过依靠ZendFrameword 的filter,validator组件,实现了对字段的验证和检查

使用方式:

通过ZF注册命名空间,同时添加库路径到include_path,然后实例Connection类, 此时并没有连接Mongo,只有到真正请求的时候才会连接

If you are connecting to localhost without any authentication then no need to worry about connections any further. Shanty Mongo will connect automatically on the first request if no connections have previously been added.

$loader->registerNamespace('Shanty_Mongo');
$master = new Shanty_Mongo_Connection('mongodb://localhost:27017');
Shanty_Mongo::addMaster($master);

下面可以编写一个Document类

先建立一个mongo collection,使用mongo shell : use user, collection自动创建

创建User类,这里仅仅实现基础的功能

<?php
require_once "Shanty/Mongo/Document.php";

class Mon_User extends Shanty_Mongo_Document 
{
    protected static $_db = 'shanty';
    protected static $_collection = 'user';
	
    protected static $_requirements = array(
        'name' => 'Required',
        'email' => array('Required', 'Validator:EmailAddress'),
        'friends' => 'DocumentSet',
        'friends.$' => array('Document:Mon_User')
    );
}

在Zend Controller中编写一个实例来测试下

public function indexAction() {
		$this->_helper->viewRenderer->setNoRender(true);
		try {
			$user = new Mon_User();			
			$user->name = "me";
			$user->email = "si@ekc.com";			
						
			$user->save();
		} catch(Exception $e) {
			echo "<pre style=\"color:red;\">",$e,"</pre>";
		}
	}
这样就完成一条Document保存,到Mongo命令行看看,db.user.find(), 有记录了,下面修改下代码,更新下刚才的记录

$user = new Mon_User();
$user->update(array('name'=>'me'), array('$set' => array("email"=>"sdfsf@dk.com")), array('multiple'=>true));
现在可以检查下是否更新,这个语句可以更新多条记录,这个实际上就是封装的mongo扩展,类中实现:

return static::getMongoCollection(true)->update($criteria, $object, $options);

下面使用下文档集的概念

$user = new Mon_User();
$friends = $user->one(array('name'=>'me'), array('name', 'email'));
$user->name = "me";
$user->email = "si@ekc.com";
$friend = $user->friends->new();
$friend->name = $friends->getProperty('name');
$friend->email = $friends->getProperty('email');
$user->friends->addDocument($friend);

$user->save();
这样就可以给一个字段添加文档集了


未完待续。。。


参考:

https://github.com/coen-hyde/Shanty-Mongo

http://www.maltblue.com/zend-framework/zend-framework-and-mongodb-%E2%80%93-how-to-put-them-together




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值