Download the PHP package sokil/php-mongo without Composer
On this page you can find all versions of the php package sokil/php-mongo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-mongo
Stand With Ukraine
PHPMongo ODM
PHP ODM for MongoDB.
:star: Why to use this ODM? You can easily work with document data through comfortable getters and setters instead of array and don't check if key exist in array. Access to sub document uses dot-syntax. You can validate data passed to document before save. We give you events, which you can handle in different moments of document's life. You can create relations, build aggregations, create versioned documents, write migrations and do more things which makes your life easier.
:1234: Tested over MongoDB v.2.4.12, v.2.6.9, v.3.0.2, v.3.2.10, v.3.3.15, v.3.4.0. See Unit tests for details.
Requirements
- PHP-Mongo v.1
- PHP 5.3 not supported starting from 2018-10-19
- PHP 5.4 - 5.5 not supported starting from 2019-10-19
- PHP 5.6
- PHP Mongo Extension 0.9 or above (Some features require >= 1.5)
- PHP 7
- PHP MongoDB Extension 1.0 or above
- Compatibility layer. Please, note some restriontions
- HHVM Driver not supported.
- PHP-Mongo v.2 (Currentry is under development and can not be used in production)
- PHP 7
- PHP MongoDB Extension natively without legacy driver adapters.
- PHP 5 not supported
- HHVM Driver not supported.
Table of contents
- Installation
- Common installation
- Symfony bundle
- Laravel
- Yii component
- Yii2 component
- Support of migrations
- Connecting
- Mapping
- Selecting database and collection
- Custom collections
- Document schema
- Document validation
- Getting documents by id
- Create new document
- Get and set data in document
- Embedded documents
- Get embedded document
- Set embedded document
- Get embedded list of documents
- Set embedded list of documents
- Validation of embedded documents
- DBRefs
- Storing document
- Storing mapped object
- Insert and update documents without ODM
- Batch insert
- Batch update
- Moving data between collections
- Querying documents
- Query Builder
- Extending Query Builder
- Identity Map
- Comparing queries
- Geospatial queries
- Fulltext search
- Pagination
- Persistence (Unit of Work)
- Deleting collections and documents
- Aggregation framework
- Events
- Behaviors
- Relations
- One-to-one relation
- One-to-many relation
- Many-to-many relation
- Add relation
- Remove relation
- Concurency
- Optimistic locking
- Read preferences
- Write concern
- Capped collections
- Executing commands
- Queue
- Migrations
- GridFS
- Versioning
- Indexes
- Caching and documents with TTL
- Debugging
- Logging
- Profiling
- Unit tests
Installation
Common installation
You can install library through Composer:
Download latest release: Latest sources from GitHub
Compatibility with PHP 7
PHPMongo currently based on old ext-mongo entension. To use this ODM with PHP 7, you need to add compatibility layer, which implement API of old extension over new ext-mongodb. To start using PHPMongo with PHP7, add requirement alcaeus/mongo-php-adapter to composer. Restrictions for using ODM with compatibility layer you can read in known issues of original adapter.
Require adapter of old ext-mongo
API to new ext-mongodb
:
Symfony bundle
If you use Symfony framework, you can use Symfony MongoDB Bundle which wraps this library
Laravel
If you use Laravel framework, use Laravel adapter
Yii component
If you use Yii Framework, you can use Yii Adapter which wraps this library
This package in addition to PHPMongo adapter also has data provider and log router for MongoDb.
Yii2 component
If you use Yii2 Framework, you can use Yii2 Adapter which wraps this library
Support of migrations
If you require migrations, you can add dependency to Migrator, based on this library:
Connecting
Single connection
Connecting to MongoDB server made through \Sokil\Mongo\Client
class:
Format of DSN used to connect to server is described in PHP manual. To connect to localhost, use next DSN:
To connect to replica set, use next DSN:
Pool of connections
If you have few connections, you may prefer connection pool instead of managing different connections. Use \Sokil\Mongo\ClientPool
instance to initialize pool object:
Mapping
Selecting database and collection
You can get instances of databases and collections by its name.
To get instance of database class \Sokil\Mongo\Database
:
To get instance of collection class \Sokil\Mongo\Collection
:
Default database may be specified to get collection directly from \Sokil\Mongo\Client
object:
Custom collections
Custom collections are used to add some collection-specific features in related class.
First you need to create class extended from \Sokil\Mongo\Collection
:
This class must be then mapped to collection name in order to return object of this class when collection requested. Custom collection referenced in standard way:
Collection definition
Collection name must be mapped to collection class. If you want to pass some additional options to collection, you also can configure them in mapping definition:
All options later may be accessed by Collection::getOption()
method:
Predefined options are:
Option | Default value | Description |
---|---|---|
class | \Sokil\Mongo\Collection | Fully qualified collection class |
documentClass | \Sokil\Mongo\Document | Fully qualified document class |
versioning | false | Using document versioning |
index | null | Index definition |
expressionClass | \Sokil\Mongo\Expression | Fully qualified expression class for custom query builder |
behaviors | null | List of behaviors, attached to every document |
relations | null | Definition of relations to documents in other collection |
batchSize | null | Number of documents to return in each batch of response |
clientCursorTimeout | null | A timeout can be set at any time and will affect subsequent queries on the cursor, including fetching more results from the database |
serverCursorTimeout | null | A cumulative time limit in milliseconds to be allowed by the server for processing operations on the cursor |
documentPool | true | Document pool, used to store already fetched documents in identity map |
If class
omitted, then used standart \Sokil\Mongo\Collection
class.
To override default document class use documentClass
option of collection:
Mapping of collection name to collection class
If only class name of collection defined, you may simply pass it in mapping.
There is also deprecated method to specify collection's class name. Please, use array definition and option class
.
Mapping with class prefix
Collections not configured directly, may be mapped automatically by using *
in mapping keys.
Any collection may be mapped to class without enumerating every collection name.
There is also deprecated method to specify class prefix. Please, use *
as collection name and array definition with option class
.
Regexp mapping
Collection name in mapping may be defined as RegExp pattern. Pattern must start from symbol /
:
Any collection with name matched to pattern will be instance of \Some\Collection\Class
:
Any stored regexp values than may be get through $collection->getOption('regex');
.
Document schema and validating
Custom document class
Custom document class may be useful when required some processing of date on load, getting or save. Custom document class must extend \Sokil\Mongo\Document
.
Now you must configure its name in collection's class by overriding method Collection::getDocumentClassName()
:
Single Collection Inheritance
Often useful to have different document classes, which store data in single collection.
For example you have products in your shop Song
and VideoClip
, which inherit abstract Product
.
They have same fields like author or duration, but may also have other different fields and
behaviors. This situation described in example Product Catalog.
You may flexibly configure document's class in \Sokil\Mongo\Collection::getDocumentClassName()
relatively to concrete value of field (this field called discriminator), or other more complex logic:
Also document class may be defined in collection mapping:
In example above class \CustomVideoDocument
related to {"_id": "45..", "type": "video"}
, and \CustomAudioDocument
to {"_id": "45..", type: "audio"}
Document schema
Document's scheme is completely not required.
If field is required and has default value, it can be defined in special property of document class Document::schema
:
Also supported deprecated format Document::_data
:
Document validation
Document may be validated before save. To set validation rules, you may override method \Sokil\Mongo\Document::rules()
and pass validation rules here. Supported rules are:
Document can have validation state, based on scenario. Scenario can be specified by method Document::setScenario($scenario)
.
If some validation rule applied only for some scenarios, this scenarios must be passed on on
key, separated by comma.
If some validation rule applied to all except some scenarios, this scenarios must be passed on except
key, separated by comma.
There are two equal cases for document validation:
or
By default, document validates before save and \Sokil\Mongo\Document\InvalidDocumentException
is thrown if it invalid. Exception \Sokil\Mongo\Document\Exception\Validate
was thrown before v.1.11.6
when document was invalid. Since v.1.11.6
this exception is deprecated. Use \Sokil\Mongo\Document\InvalidDocumentException
instead.
Errors may be accessed through Document::getErrors()
method of document object.
Also instabce of document may be get from exception method:
If allowed to save invalid documents, disable validation on save:
Error may be triggered manually by calling method triggerError($fieldName, $rule, $message)
You may add you custom validation rule just adding method to document class and defining method name as rule:
You may create your own validator class, if you want to use validator in few classes.
Just extend your class from abstract validator class \Sokil\Mongo\Validator
and register your own validator namespace:
Getting documents by id
To get document from collection by its id:
To add additional checks or query modifiers use callable:
Note that if callable specified, document always loaded directly omitting document pool.
Create new document
Create new empty document object:
Or with pre-defined values:
Get and set data in document
Get
To get value of document's field you may use one of following ways:
If field not exists, null value returned.
Set
To set value you may use following ways:
Push
Push will add value to array field:
If field already exists, and not sequential list of values, then in case of scalar, scalar will be converted to array.
If values pushed to field with subdocument, then triggered Sokil\Mongo\Document\InvalidOperationException
.
Embedded documents
Get embedded document
Imagine that you have document, which represent User
model:
You can define embedded profile
document as standalone class:
Now you are able to get profile params:
Set embedded document
You can also set embedded document. If embedded document has validation rules, they will be checked before embed it to document:
If embedded document is invalid, it will throw Sokil\Mongo\Document\InvalidDocumentException
. Embedded
document may be obtained from exception object:
Get embedded list of documents
Imagine that you have stored post data in collection 'posts', and post document has embedded comment documents:
So we can create Comment
model, which extends \Sokil\Mongo\Structure
:
Now we can create Post
model with access to embedded Comment
models:
Method Post::getComments()
allows you to get all of embedded document. To
paginate embedded documents you can use \Sokil\Mongo\Cursor::slice()
functionality.
If you get Document
instance through Collection::getDocument()
you can define
additional expressions for loading it:
Set embedded list of documents
You can store embedded document to array, and validate it before pushing:
Validation of embedded documents
As embedded document is Structure
, it has all validation functionality as Document
. Currently embedded document validates only just before set to Document
or manually. If embedded document is invalid, it trowns Sokil\Mongo\Document\InvalidDocumentException
.
DBRefs
In most cases you should use the manual reference method for connecting two or more related documents - including one document’s _id field in another document. The application can then issue a second query to resolve the referenced fields as needed. See more info about supporting manual references in section Relations
However, if you need to reference documents from multiple collections, or use legacy database with DBrefs inside, consider using DBRefs. See more info about DBRef at https://docs.mongodb.com/manual/reference/database-references/.
If you have DBRef array, you can get document instance:
Adding reference to one document in another:
Get document from reference field:
Push relation to list of relations:
Get list of related documents:
List of references may be from different collections of same database.
Specifying of database in reference not supported.
Storing document
Storing mapped object
If you have previously loaded and modified instance of \Sokil\Mongo\Document
, just save it.
Document will automatically be inserted or updated if it already stored.
Insert and update documents without ODM
If required quick insert of document without validating, events just insert it as array:
To update existed documents, use:
Expression may be defined as array, \Sokil\Mongo\Expressin
object or callable, which configure expression.
Operator may be defined as array, \Sokil\Mongo\Operator
object or callable which configure operator.
Options is array of all allowed options, described in http://php.net/manual/ru/mongocollection.update.php.
For example:
Batch insert
To insert many documents at once with validation of inserted document:
Also supported \MongoInsertBatch
through interface:
Batch update
Making changes in few documents:
To update all documents use:
Rename fields of documents:
Also supported \MongoUpdateBatch
through interface:
Moving data between collections
To copy documents from one collection to another according to expression:
To move documents from one collection to another according to expression:
Important to note that there is no transactions so if error will occur
during process, no changes will rollback.
Querying documents
Query Builder
To query documents, which satisfy some conditions you need to use query builder:
All "where" conditions added with logical AND. To add condition with logical OR:
Result of the query is iterator \Sokil\Mongo\Cursor
, which you can then iterate:
Or you can get result array:
To get only one result:
To get only one random result:
To get values from a single field in the result set of documents:
To map found documents:
To filter found documents:
To apply chain of functions to result, use ResultSet
:
When iterating through cursor client retrieve some amount of documents from the server in one round trip. To define this numner of documents:
Query timeouts
Client timeout defined to stop waiting for a response and throw a \MongoCursorTimeoutException after a set time. A timeout can be set at any time and will affect subsequent queries on the cursor, including fetching more results from the database.
Server-side timeout for a query specifies a cumulative time limit in milliseconds to be allowed by the server for processing operations on the cursor.
Distinct values
To get distinct values of field:
Values may be filtered by expression specified as array, callable or Expression
object:
Extending Query Builder
For extending standart query builder class with custom condition methods you need to create expression class which extends \Sokil\Mongo\Expression
:
And then specify it in collection mapping:
Also there is deprecated feature to override property Collection::$_queryExpressionClass
:
Now new expression methods available in the query buiilder:
Identity Map
Imagine that you have two different query builders and they are both return same document. Identity map helps us to get same instance of object from different queries, so if we made changes to document from first query, that changes will be in document from second query:
This two documents referenced same object. Collection by default store all requested documents to identity map.
If we obtain document directly by id using Collection::getDocument()
and document was previously loaded to identity map, it will be fetched from identity map without requesing database. Even document present in identity map, it can be fetched direcly from db by using Collection::getDocumentDirectly()
with same syntax as Collection::getDocument().
If serial requests fetch same document, this document not replaced in identity mav, but content of this document will be renewed. So different requests works with same document stored in identity map.
If we know that documents never be reused, we can disable storing documents to identity map:
Document pool may be disabled or enabled in mapping. By default it is enabled:
To enable identity mapping:
To check if identity mapping enabled:
To clear pool identity map from previously stored documents:
To check if there are documents in map already:
If document already loaded, but it may be changed from another proces in db, then your copy is not fresh. You can manually refresh document state syncing it with db:
Comparing queries
If you want to cache your search results or want to compare two queries, you need some
identifier which unambiguously identify query. You can use Cursor::getHash()
for
that reason. This hash uniquely identify just query parameners rather
than result set of documents, because it calculated from all query parameters:
Geospatial queries
Before querying geospatial coordinates we need to create geospatial index and add some data.
Index 2dsphere available since MongoDB version 2.4 and may be created in few ways:
Geo data can be added as array in GeoJson format or using GeoJson objects of library GeoJson:
Add data as GeoJson object
Data may be set througn array:
Query documents near point on flat surface, defined by latitude 49.588264 and longitude 34.551416 and distance 1000 meters from this point:
This query require 2dsphere
or 2d
indexes.
Distance may be specified as array [minDistance, maxDistance]
. This
feature allowed for MongoDB version 2.6 and greater. If some value
empty, only existed value applied.
To search on spherical surface:
To find geometries, which intersect specified:
To select documents with geospatial data that exists entirely within a specified shape:
Search documents within flat circle:
Search document within spherical circle:
Search documents with points (stored as legacy coordinates) within box:
Search documents with points (stored as legacy coordinates) within polygon:
Fulltext search
Before search field must be previously indexed as fulltext search field:
Searching on fulltext field:
Pagination
Query builder allows you to create pagination.
Persistence (Unit of Work)
Instead of saving and removing objects right now, we can queue this job and execute all changes at once. This may be done through well-known pattern Unit of Work. If installed PHP driver above v. 1.5.0 and version of MongoDB above, persistence will use MongoWriteBatch
classes, which can execute all operations of same type and in same collection at once.
Lets create persistance manager
Now we can add some documents to be saved or removed later
If later we decice do not save or remove document, we may detach it from persistence manager
Or we even may remove them all:
Note that after detaching document from persistence manager, it's changes do not removed and document still may be saved directly or by adding to persistence manager.
If we decide to store changes to databasae we may flush this changes:
Note that persisted documents do not deleted from persistence manager after flush, but removed will be deleted.
Deleting collections and documents
Deleting of collection:
Deleting of document:
Deleting of few documents by expression:
Also supported \MongoDeleteBatch
through interface:
Aggregation framework
Create aggregator: `
Than you need to configure aggregator by pipelines.
To get results of aggregation after configuring pipelines:
You can execute aggregation without previously created aggregator:
Options
Available aggregation options may be found at https://docs.mongodb.org/manual/reference/command/aggregate/#dbcmd.aggregate.
Options may be passed as argument of aggregate
method:
Debug
If client in debug mode and logger configured, pipelines will be logged. There is ability to get explanation of aggregation:
Aggregation cursor
Collection::aggregate
return array as result, but also iterator may be optained:
Read more at http://php.net/manual/ru/mongocollection.aggregatecursor.php.
Events
Event support based on Symfony's Event Dispatcher component. You can attach and trigger any event you want, but there are some already defined events:
Event name | Description |
---|---|
afterConstruct | Already after construct executed |
beforeValidate | Before document validation |
afterValidate | After document validation |
validateError | After document validation when document is invalid |
beforeInsert | Before document will insert to collection |
afterInsert | After successfull insert |
beforeUpdate | Before document will be updated |
afterUpdate | After successfull update of document |
beforeSave | Before insert or update of document |
afterSave | After insert or update of document |
beforeDelete | Before delete of document |
afterDelete | After delete of document |
Event listener is a function that calls when event triggered:
Event listener may be attached by method Document::attachEvent()
:
It also may be attached through helper methods:
Event may be attached in runtime or in Document
class by override Document::beforeConstruct()
method:
Event may be triggered to call all attached event listeners:
You can create your own event class, which extends `\Sokil\Mongo\Event' and pass it to listeners. This allows you to pass some data to listener:
To cancel operation execution on some condition use event handling cancel:
Behaviors
Behavior is a posibility to extend functionality of document object and reuse code among documents of different class.
Behavior is a class extended from \Sokil\Mongo\Behavior
. Any public method may be
accessed through document, where behavior is attached.
To get instance of object, to which behavior is attached, call Behavior::getOwner()
method:
You can add behavior in document class:
You can attach behavior in runtime too:
Behaviors may be defined as fully qualified class names, arrays, or Behavior
instances:
Then you can call any methods of behaviors. This methods searches in order of atraching behaviors:
Relations
You can define relations between different documents, which helps you to load related documents.
To define relation to other document you need to override Document::relations()
method and return array of relations in format [relationName => [relationType, targetCollection, reference], ...]
.
Also you can define relations in mapping:
If relation specified both in mapping and document class, then mapping relation merged into document's relations, so mapping relations has more priority.
One-to-one relation
We have to classes User and Profile. User has one profile, and profile belongs to User.
Now we can lazy load related documnts just calling relation name:
One-to-many relation
One-to-many relation helps you to load all related documents. Class User has few posts of class Post:
Now you can load related posts of document:
Many-to-many relation
Many-to-many relation in relational databases uses intermediate table with stored ids of related rows from both tables. In mongo this table equivalent embeds to one of two related documents. Element of relation definition at position 3 must be set to true in this document.
Now you can load related documents:
Add relation
There is helper to add related document, if you don't want modify relation field directly:
This helper automatically resolves collection and field where to store relation data.
Remove relation
There is helper to remove related document, if you don't want modify relation field directly:
This helper automatically resolves collection and field
where to remove relation data. If relation type is HAS_MANY
or BELONS_TO
,
second parameter wich defined related object may be omitted.
Concurency
Optimistic locking
To enable optimistic locking, specify lock mode in mapping:
Now when some process try to update already updated document, exception
\Sokil\Mongo\Document\OptimisticLockFailureException
will be thrown.
Read preferences
Read preference describes how MongoDB clients route read operations to members of a replica set. You can configure read preferences at any level:
Write concern
Write concern describes the guarantee that MongoDB provides when reporting on the success of a write operation. You can configure write concern at any level:
Capped collections
To use capped collection you need previously to create it:
Now you can add only 10 documents to collection. All old documents will ve rewritted ny new elements.
Executing commands
Command is universal way to do anything with mongo. Let's get stats of collection:
Result in $stats:
Queue
Queue gives functionality to send messages from one process and get them in another process. Messages can be send to different channels.
Sending message to queue with default priority:
Send message with priority
Reading messages from channel:
Number of messages in queue
Migrations
Migrations allows you easily change schema and data versions. This functionality implemented in packet https://github.com/sokil/php-mongo-migrator and can be installed through composer:
GridFS
GridFS allows you to store binary data in mongo database. Details at http://docs.mongodb.org/manual/core/gridfs/.
First get instance of GridFS. You can specify prefix for partitioning filesystem:
Now you can store file, located on disk:
You can store file from binary data:
You are able to store some metadata with every file:
Get file by id:
Find file by metadata:
Deleting files by id:
Reading of file content
If you want to use your own GridFSFile
classes, you need to define mapping, as it does with collections:
Versioning
Versioninbg allows you to have history of all document changes. To enable versioning of documents in collection, you can set protected
property Collection::$versioning
to true
, call Collection::enableVersioning()
method or define versioning in mapping.
To check if documents in collections is versioned call:
Revision is an instance of class \Sokil\Mongo\Revision
and inherits \Sokil\Mongo\Document
,
so any methods of document may be applied to revision. Revisions may be accessed:
To get one revision by id use:
To get count of revisions:
To clear all revisions:
Revisions stored in separate collection, named {COLLECTION_NAME}.revisions
To obtain original document of collection {COLLECTION_NAME}
from revision,
which is document of collection {COLLECTION_NAME}.revisions
,
use Revision::getDocument()
method:
Properties of document from revision may be accessed directly:
Also date of creating revison may be obtained from document:
Indexes
Create index with custom options (see options in http://php.net/manual/en/mongocollection.ensureindex.php):
Create unique index:
Create sparse index (see http://docs.mongodb.org/manual/core/index-sparse/ for details about sparse indexes):
Create TTL index (see http://docs.mongodb.org/manual/tutorial/expire-data/ for details about TTL indexes):
You may define field as array where key is field name and value is direction:
Also you may define compound indexes:
You may define all collection indexes in property Collection::$_index
as array, where each item is an index definition.
Every index definition must contain key keys
with list of fields and orders,
and optional options, as described in http://php.net/manual/en/mongocollection.createindex.php.
Then you must create this indexes by call of Collection::initIndexes()
:
You may use Mongo Migrator package to ensure indexes in collections from migration scripts.
Query optimiser automatically choose which index to use, but you can manuallty define it:
Caching and documents with TTL
If you want to get collection where documents will expire after some specified time, just add special index to this collection.
You can do this also in migration script, using Mongo Migrator. For details see related documentation.
You also can use \Sokil\Mongo\Cache
class, which already implement this functionality and compatible with PSR-16 interface.
Namespace is a name of collection to be created in database.
Before use cache must be initialised by calling method Cache:init()
:
<?php
$cahce->init();
This operation creates index with expireAfterSecond
key in collection some_namespace
.
This operation may be done in some console command or migration script, e.g. by using migration tool sokil/php-mongo-migrator, or you can create manually in mongo console:
db.some_namespace.ensureIndex('e', {expireAfterSeconds: 0});
Now you can store new value with:
<?php
// this store value for 10 seconds
// expiration defined relatively to current time
$cache->set('key', 'value', 10);
You can define value which never expired and must be deleted manually:
<?php
$cache->set('key', 'value', null);
You can define some tags defined with key:
<?php
$cache->set('key', 'value', 10, ['php', 'c', 'java']);
To get value
<?php
$value = $cache->get('key');
To delete cached value by key:
<?php
$cache->delete('key');
Delete few values by tags:
<?php
// delete all values with tag 'php'
$cache->deleteMatchingTag('php');
// delete all values without tag 'php'
$cache->deleteNotMatchingTag('php');
// delete all values with tags 'php' and 'java'
$cache->deleteMatchingAllTags(['php', 'java']);
// delete all values which don't have tags 'php' and 'java'
$cache->deleteMatchingNoneOfTags(['php', 'java']);
// Document deletes if it contains any of passed tags
$cache->deleteMatchingAnyTag(['php', 'elephant']);
// Document deletes if it contains any of passed tags
$cache->deleteNotMatchingAnyTag(['php', 'elephant']);
Debugging
In debug mode client may log some activity to pre-configured logger or show extended errors.
<?php
// start debugging
$client->debug();
// stop debugging
$client->debug(false);
// check debug state
$client->isDebugEnabled();
Logging
Library suports logging of queries. To configure logging, you need to pass logger object to instance of \Sokil\Mongo\Client
and enable debug of client.
Logger must implement \Psr\Log\LoggerInterface
due to PSR-3:
<?php
$client = new Client($dsn);
$client->setLogger($logger);
Profiling
More details about profiling at Analyze Performance of Database Operations
profiler data stores to system.profile
collection, which you can query through query builder:
<?php
$qb = $database
->findProfilerRows()
->where('ns', 'social.users')
->where('op', 'update');
Structure of document described in article Database Profiler Output
There is three levels of profiling, described in article Profile command. Switching between then may be done by calling methods:
<?php
// disable profiles
$database->disableProfiler();
// profile slow queries slower than 100 milliseconds
$database->profileSlowQueries(100);
// profile all queries
$database->profileAllQueries();
To get current level of profiling, call:
<?php
$params = $database->getProfilerParams();
echo $params['was'];
echo $params['slowms'];
// or directly
$level = $database->getProfilerLevel();
$slowms = $database->getProfilerSlowMs();
Unit tests
Local PHPUnit tests
To start unit tests, run:
./vendor/bin/phpunit -c tests/phpunit.xml tests
Docker PHPUnit tests
Also available Docker containers. They start with xdebug enabled, so you can sign in to any container and debug code there.
To start tests on all supported PHP and MongoDB versions, run
./run-docker-tests.sh
To run test on concrete platforms, specify them:
./run-docker-tests.sh -p 56 -p 70 -m 30 -m 32
To run concrete test, pass it:
./run-docker-tests.sh -t DocumentTest.php
To run concrete method of test, pass it:
./run-docker-tests.sh -t DocumentTest.php -f ::testElemMatch
Tests may be found at ./share/phpunit
dir after finishing tests.
Contributing
Pull requests, bug reports and feature requests is welcome. Please see CONTRIBUTING for details.
Change log
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
All versions of php-mongo with dependencies
psr/log Version ~1.0
psr/simple-cache Version ^1.0
jmikola/geojson Version ~1.0
symfony/event-dispatcher Version >=2.0 <5.0
symfony/polyfill-php55 Version ~1.0