Download the PHP package martynbiz/php-mongo without Composer
On this page you can find all versions of the php package martynbiz/php-mongo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download martynbiz/php-mongo
More information about martynbiz/php-mongo
Files in martynbiz/php-mongo
Package php-mongo
Short Description PHP ODM for MongoDB.
License MIT
Homepage http://github.com/martynbiz/php-mongo
Informations about the package php-mongo
Installation
Install with composer
Getting started
Create connection
Create model
Create models by extending the Mongo class, be sure to define $collection and $whitelist:
Create multiple connections
If only using a single database, this is not neccessary. However, if you need to connect to multiple databases, give a unique name to each connection:
Also, remember to declare which $conn in the model:
Queries
Find by mongo query
Find one by mongo query
Finding by object
Model instances can also be passed, the find method will convert it to DBRef internally (e.g. 'friend' => $friend will do the same as 'friend' => $friend->getDBRef())
Getting and setting values
Inserting
Save method is used on an instantiated object of the model class. It can be called after assigning values to properties, or by passing name/values as arguments. Note: when passing name/values, values will be whitelisted
Create method doesn't require an instance, by will accept name\values upon which it will insert into the collection. It will return an instance of the created document. Note: when passing name/values, values will be whitelisted
Factory method doesn't actually insert, but will generate an instance with values. It can then be altered and inserted with it's save method:
Although having multiple methods may seem a little much, it does give the option to keep you code tidy and more flexible for mocking methods during testing.
Push
Note: push with $each is the default behaviour here, although this can be overridden with 'each'=>false in the options array (see below)
Deleting
An instance can delete itself from the database with the delete method:
To delete multiple documents from a collection by query, use the remove method:
Convert to array
Validation
Before saving to the database, the validate method is called. If it returns false, the data will not be saved.
The following example uses martynbiz/php-validator library here.
Custom Getter/ Setter
To automatically convert values when getting, you can define custom methods to automatically convert the value of a property. This may be useful when formatting strings such as dates to human readable.
TODO
- dot syntax support? find(array('model.name' => 'Martyn')), set/get(array(model.name' => 'Martyn'))