webcodr/mango
Composer 安装命令:
composer require webcodr/mango
包简介
A MongoDB object document mapper for PHP
README 文档
README
A MongoDB object document mapper for PHP
Inspired by Mongoid for Ruby
Requirements
- PHP 5.4
- MongoDB driver for PHP (min. 1.2.0)
- Composer
Setup
Add Mango to your project
$ php composer.phar require webcodr/mango:*
Create a document class
<?php namespace Mango\Tests\Document; use Mango\Document; use Mango\DocumentInterface; class User implements DocumentInterface { use Document; private function addFields() { $this->addField('name', ['type' => 'String']); $this->addField('email', ['type' => 'String']); $this->addField( 'created_at', [ 'type' => 'DateTime', 'index' => true, 'default' => 'now' ] ); $this->addField( 'updated_at', [ 'type' => 'DateTime', 'index' => true, 'default' => 'now' ] ); } }
You don't have to set a collection name. Mango uses the class name in lower case as collection name.
If you want to set a custom collection name, just override the method getCollectionName() in your own document classes.
There's no need to provide an id. Mango's document base class adds automatically the property '_id' with a fresh MongoId object.
Save a document
<?php use Mango\Mango; use Mango\DocumentManager; use Document\User; $mango = new Mango('mongodb://devserver:27017/galactica-actual'); $dm = new DocumentManager($mango); $user = new User(); $user->name = 'William Adama'; $user->email 'william.adama@galactica.colonial-forces.gov'; $user->store();
Remove a document
$user->remove();
Querying
The methods find and where return a \Mango\Persistence\Cursor object or an object of the class \Collection\MutableMap. It depends on which method is called.
MutableMap is part of another WebCodr project called Collection. It provides several classes to replace PHP arrays and is much more fun to use. Check it out here.
Mango uses object hydration to automatically provide a result with document objects.
Find documents by id
One id
$user = User::find('abc')->first();
Multiple ids
$user = User::find('abc', 'def', 'ghi')->first();
Find all documents in collection
User::where()->each(function($user) { echo $user->name; });
Find a document with certain field value
$user = User::where(['name' => 'William Adama']); echo $user->count(); // result = 1 echo $user->first()->email; // result = william.adama@galactica.colonial-forces.gov
统计信息
- 总下载量: 102
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-03-15