Download the PHP package jpc/mongodb-odm without Composer
On this page you can find all versions of the php package jpc/mongodb-odm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mongodb-odm
MongoDB-ODM
JPC\MongoDB-ODM is a smart MongoDB-PHP mappers. It lets you to get your Object in PHP mapped into document in MongoDB. This library support last driver of mongodb available here.
See 'doc' folder for more details.
Install
Install With Composer
In your console terminal :
Or in your :
Create Mapping Class
Your class
To work with MongoDB-ODM, you need to see your class like a mongoDb document and define what you need :
- an id
- somes fields
You can create a simple object like this
Ok, you have your class. Now you have to tell to MongoDB-ODM which property match which field, and in which collection document are (and will be) stored.
Mapping
For this, MongoDB-ODM use annotations. There is basics annotations to make a mapping that are in JPC\MongoDB\ODM\Annotations\Mapping
:
Document
: define the collection infosId
: define the property that will match mongoDb_id
field-
Field
: define a field mappingNow, edit your object like this :
Advanced Mapping
Ok, you have a simple document but it still to miss something... Maybe things like embedded documents. Don't worry, MongoDB-ODM support them!
Create your embedded class like the previous one :
You can now use two annotations to add embedded in the previous class :
EmbeddedDocument
: Embed one document of specified typeMultiEmbeddedDocument
: Embed an array of document of specified type
Modify the class MyDoc
like this :
Get document
Get a document manager
You can easily create a document manager with the factory available in the library.
The factory has a method createDocumentManager
that take mongo uri in first argument, and database name in second.
Now, create a document manager :
Get the repository
Now, you need to get the repository. Repository will allow you to get document from MongoDB.
To get Repository use the function getRepository
method of document manager.
This is the method :
If $collection
is null, MongoDB-ODM take the collection setted in the Document
annotation of the model
In our case (With model configured in 2-create-mapping-class
) :
You can now simply find a document by using one of this functions of repository (See method declaration for more infos about methods):
Example :
Insert, update or remove a document
Insert a document
First, you have to create your object and insert data inside it :
Good! Now we want to store it in database. For this, you have to tell to MongoDB-ODM that your object have to be managed by him. By manage
i mean that MongoDB-ODM will see if it has to insert, update or remove your object.
So, to do this, you need to persist
your object.
Here is the persist
function of document manager :
For our object :
Next step is tell to MongoDB-ODM that it has to save all changes of all managed object. For this, there is a function called flush
, just call it.
Check your mongoDb collection, if it's empty, you probably make a mistake :confused:
Update a document
Imagine that you have a lot of document in your collection and you want to update some of them.
First, you have to get the document that you want, like this :
Note : If you have inserted a document with
persist
andflush
, it is ready for update, you don't need to get it from repository again
Now make some modification on them :
This is really nice modification :wink:
Now you just have to flush
.
Note : Finded object from repositories are already persisted, you don't have to persist again
Remove a document
Removing is easy like updating
Get your document from repository and just say to document manager that it has to remove them and flush:
Additional information
Advantage of using update with MongoDB-ODM
Advantage of using MongoDB-ODM for update is that it will just update modified field and not the whole object.
Unpersist
If you make some modification on persisted object in your code and you don't want to store changes at next flush, you can unpersist it with the unpersist
function :
If you want to unpersist all objects, use clear
GridFS Document
Create mapping for GridFS document
Your class need to inherit class JPC\MongoDB\ODM\GridFS\Document
. This class define all default field of a gridFS document like md5
, filename
, etc... See this class for more details.
To add metadata to your doc, you need to use the Metadata
annotations.
Here is an example :
Note that class
Document
annotation is provided fromGFS
and not byODM
namespace
Insert your document
Insert a document in gridFS is like insert a document in basic mongoDB collection.
Here's a full example :
Collections Options
Collections options
When you create a model, you can specify more information on the Document
annotion :
And you can set ReadConcern, ReadPreference, TypeMap and WriteConcern with the Option
annotation (Don't forget the use) :
Events
Availabe events
Name | Description |
---|---|
PostLoad | Called after the document was loaded from database |
PrePersist | Called before persist executed |
PostPersist | Called after persist executed |
PreFlush | Called before flush executed (Even if document has not changed) |
PostFlush | Called after flush executed (Even if document has not changed) |
PreInsert | Called before document is inserted in database |
PostInsert | Called after document is inserted in database |
PreUpdate | Called before document is updated in database |
PostUpdate | Called after document is updated in database |
PreDelete | Called after document is updated in database |
PostDelete | Called after document is updated in database |
How to use
To add event on your model class, you need to tell ODM that you want to use some event with HasLifecycleCallbacks
annotation
You can now add some methods with events annotations
All versions of mongodb-odm with dependencies
doctrine/annotations Version ^1.4|^1.6
doctrine/cache Version ^1.6|^1.7|^1.8
mongodb/mongodb Version ^1.4