📜 ⬆️ ⬇️

PHP MongoDB ORM

php + mongo
MongoDB is a document-based database management system that stores data in the form of sets of JSON-like documents. To work with MondoDB in PHP, the peg mongo extension is used , which allows you to fully work with the DBMS using access objects.
The article will discuss the ORM (Object-relational mapping) standalone libraries and frameworks that allow you to simplify the use of Mongo in PHP projects and provide an interface to work with data.

I decided not to describe how to work with Mongo in CMS and frameworks (such as ZendFramework, Symfony, Drupal, etc.), because their configuration is a task specific only to developers using these frameworks, but the purpose of the article is to highlight universal tools.

The entire list of libraries and frameworks is on the MondoDB website , I will focus on the following three, with the most extensive functionality:
  1. Doctrine orm
  2. Mandango
  3. MongoRecord

Doctrine orm

MongoDB Object Document Mapper is still at the beta stage, but already quite functional, it allows using Doctrine methods (annotations, XML or YAML) that are familiar to PHP map objects into Mongo documents. Provides a convenient interface for using indexes, query designer, MapReduce. Accompanied by rather abundant documentation .
A big plus is the ability to use annotations for mapping:
/** @Document */ class User { // ... /** @Field(type="string") */ private $username; } 


Mandango

A rather young ODM (Object Document Mapper), which focuses on high performance and extensibility. Judging by the speed comparisons of Doctrine and Mandango (on the Mandango website), the latter, on average, works 4 times faster, providing an almost similar package of features.
In addition, it has a built-in event manager and can store files in the GridFS using the MongoGridFS extension. The documentation is pretty good.
Mapping is set in the configuration using arrays:
 array( 'Model\Article' => array( 'fields' => array( // as string 'title' => 'string', // as array 'content' => array('type' => 'string'), ), ), ); 

')
MongoRecord

A simple library consisting of 4 classes, which allows inheriting the base class to add functions to work with the DBMS to its classes. It does not provide mapping, query design and other advanced features. Simple and angry.

Conclusion

Yes, the choice is not great. For a project that needs an ORM with broad functionality, either Doctrine or Mandango will do. The latter is really quite fast, because it is sharpened just under Mongo and is not able to do anything except work with it, but it copes well with this task. The rest of the libraries either died unborn or provide only an interface for working with standard PHP classes.

PS If you know a decent ORM framework not covered in the article, write in the comments or lichku, I will add. Because the Russian-language information is quite small, we will fill this gap together.

Source: https://habr.com/ru/post/134740/


All Articles