Download the PHP package webcodr/mango without Composer
On this page you can find all versions of the php package webcodr/mango. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webcodr/mango
More information about webcodr/mango
Files in webcodr/mango
Package mango
Short Description A MongoDB object document mapper for PHP
License MIT
Homepage https://github.com/WebCodr/Mango
Informations about the package mango
Mango
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
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 '[email protected]';
$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 = [email protected]