class DocumentManager implements ObjectManager (View source)

The DocumentManager class is the central access point for managing the persistence of documents.

<?php

$config = new Configuration();
$dm = DocumentManager::create(new Connection(), $config);

Constants

CLIENT_TYPEMAP

Methods

__construct(Client|null $client = null, Configuration|null $config = null, EventManager|null $eventManager = null)

Creates a new Document that operates on the given Mongo connection and uses the given Configuration.

getProxyFactory()

Gets the proxy factory used by the DocumentManager to create document proxies.

static 
create(Client|null $client = null, Configuration|null $config = null, EventManager|null $eventManager = null)

Creates a new Document that operates on the given Mongo connection and uses the given Configuration.

getEventManager()

Gets the EventManager used by the DocumentManager.

getClient()

Gets the MongoDB client instance that this DocumentManager wraps.

getMetadataFactory()

Gets the metadata factory used to gather the metadata of classes.

initializeObject(object $obj)

Helper method to initialize a lazy loading proxy or persistent collection.

getUnitOfWork()

Gets the UnitOfWork used by the DocumentManager to coordinate operations.

getHydratorFactory()

Gets the Hydrator factory used by the DocumentManager to generate and get hydrators for each type of document.

getSchemaManager()

Returns SchemaManager, used to create/drop indexes/collections/databases.

getClassNameResolver()

Returns the class name resolver which is used to resolve real class names for proxy objects.

getClassMetadata(string $className)

Returns the metadata for a class.

getDocumentDatabase(string $className)

Returns the MongoDB instance for a class.

Database[]
getDocumentDatabases()

Gets the array of instantiated document database instances.

getDocumentCollection(string $className)

Returns the collection instance for a class.

getDocumentBucket(string $className)

Returns the bucket instance for a class.

Collection[]
getDocumentCollections()

Gets the array of instantiated document collection instances.

createQueryBuilder(string[]|string|null $documentName = null)

Create a new Query instance for a class.

createAggregationBuilder(string $documentName)

Creates a new aggregation builder instance for a class.

persist(object $document)

Tells the DocumentManager to make an instance managed and persistent.

remove(object $document)

Removes a document instance.

refresh(object $document)

Refreshes the persistent state of a document from the database, overriding any local changes that have not yet been persisted.

detach(object $document)

Detaches a document from the DocumentManager, causing a managed document to become detached. Unflushed changes made to the document if any (including removal of the document), will not be synchronized to the database.

object
merge(object $document)

Merges the state of a detached document into the persistence context of this DocumentManager and returns the managed copy of the document.

lock(object $document, int $lockMode, int|null $lockVersion = null)

Acquire a lock on the given document.

unlock(object $document)

Releases a lock on the given document.

ObjectRepository
getRepository(string $documentName)

Gets the repository for a document class.

flush(array $options = [])

Flushes all changes to objects that have been queued up to now to the database.

getReference(string $documentName, $identifier)

Gets a reference to the document identified by the given type and identifier without actually loading it.

getPartialReference(string $documentName, $identifier)

Gets a partial reference to the document identified by the given type and identifier without actually loading it, if the document is not yet loaded.

object
find(string $documentName, mixed $identifier, int $lockMode = LockMode::NONE, int $lockVersion = null)

Finds a Document by its identifier.

clear(string|null $documentName = null)

Clears the DocumentManager.

close()

Closes the DocumentManager. All documents that are currently managed by this DocumentManager become detached. The DocumentManager may no longer be used after it is closed.

bool
contains(object $document)

Determines whether a document instance is managed in this DocumentManager.

getConfiguration()

Gets the Configuration used by the DocumentManager.

mixed
createReference(object $document, array $referenceMapping)

Returns a reference to the supplied document.

isOpen()

Check if the Document manager is open or closed.

getFilterCollection()

Gets the filter collection.

Details

protected __construct(Client|null $client = null, Configuration|null $config = null, EventManager|null $eventManager = null)

Creates a new Document that operates on the given Mongo connection and uses the given Configuration.

Parameters

Client|null $client
Configuration|null $config
EventManager|null $eventManager

getProxyFactory()

Gets the proxy factory used by the DocumentManager to create document proxies.

static create(Client|null $client = null, Configuration|null $config = null, EventManager|null $eventManager = null)

Creates a new Document that operates on the given Mongo connection and uses the given Configuration.

Parameters

Client|null $client
Configuration|null $config
EventManager|null $eventManager

getEventManager()

Gets the EventManager used by the DocumentManager.

getClient()

Gets the MongoDB client instance that this DocumentManager wraps.

ClassMetadataFactory getMetadataFactory()

Gets the metadata factory used to gather the metadata of classes.

Return Value

ClassMetadataFactory

initializeObject(object $obj)

Helper method to initialize a lazy loading proxy or persistent collection.

This method is a no-op for other objects.

Parameters

object $obj

getUnitOfWork()

Gets the UnitOfWork used by the DocumentManager to coordinate operations.

getHydratorFactory()

Gets the Hydrator factory used by the DocumentManager to generate and get hydrators for each type of document.

getSchemaManager()

Returns SchemaManager, used to create/drop indexes/collections/databases.

getClassNameResolver()

Returns the class name resolver which is used to resolve real class names for proxy objects.

getClassMetadata(string $className)

Returns the metadata for a class.

Parameters

string $className The class name.

getDocumentDatabase(string $className)

Returns the MongoDB instance for a class.

Parameters

string $className

Database[] getDocumentDatabases()

Gets the array of instantiated document database instances.

Return Value

Database[]

getDocumentCollection(string $className)

Returns the collection instance for a class.

Parameters

string $className

Exceptions

MongoDBException When the $className param is not mapped to a collection.

getDocumentBucket(string $className)

Returns the bucket instance for a class.

Parameters

string $className

Exceptions

MongoDBException When the $className param is not mapped to a collection.

Collection[] getDocumentCollections()

Gets the array of instantiated document collection instances.

Return Value

Collection[]

createQueryBuilder(string[]|string|null $documentName = null)

Create a new Query instance for a class.

Parameters

string[]|string|null $documentName (optional) an array of document names, the document name, or none

createAggregationBuilder(string $documentName)

Creates a new aggregation builder instance for a class.

Parameters

string $documentName

persist(object $document)

Tells the DocumentManager to make an instance managed and persistent.

The document will be entered into the database at or before transaction commit or as a result of the flush operation.

NOTE: The persist operation always considers documents that are not yet known to this DocumentManager as NEW. Do not pass detached documents to the persist operation.

Parameters

object $document The instance to make managed and persistent.

Exceptions

InvalidArgumentException When the given $document param is not an object.

remove(object $document)

Removes a document instance.

A removed document will be removed from the database at or before transaction commit or as a result of the flush operation.

Parameters

object $document The document instance to remove.

Exceptions

InvalidArgumentException When the $document param is not an object.

refresh(object $document)

Refreshes the persistent state of a document from the database, overriding any local changes that have not yet been persisted.

Parameters

object $document The document to refresh.

Exceptions

InvalidArgumentException When the given $document param is not an object.

detach(object $document)

Detaches a document from the DocumentManager, causing a managed document to become detached. Unflushed changes made to the document if any (including removal of the document), will not be synchronized to the database.

Documents which previously referenced the detached document will continue to reference it.

Parameters

object $document The document to detach.

Exceptions

InvalidArgumentException When the $document param is not an object.

object merge(object $document)

Merges the state of a detached document into the persistence context of this DocumentManager and returns the managed copy of the document.

The document passed to merge will not become associated/managed with this DocumentManager.

Parameters

object $document The detached document to merge into the persistence context.

Return Value

object The managed copy of the document.

Exceptions

LockException
InvalidArgumentException If the $document param is not an object.

lock(object $document, int $lockMode, int|null $lockVersion = null)

Acquire a lock on the given document.

Parameters

object $document
int $lockMode
int|null $lockVersion

Exceptions

InvalidArgumentException
LockException

unlock(object $document)

Releases a lock on the given document.

Parameters

object $document

ObjectRepository getRepository(string $documentName)

Gets the repository for a document class.

Parameters

string $documentName The name of the Document.

Return Value

ObjectRepository The repository.

flush(array $options = [])

Flushes all changes to objects that have been queued up to now to the database.

This effectively synchronizes the in-memory state of managed objects with the database.

Parameters

array $options Array of options to be used with batchInsert(), update() and remove()

Exceptions

MongoDBException

getReference(string $documentName, $identifier)

Gets a reference to the document identified by the given type and identifier without actually loading it.

If partial objects are allowed, this method will return a partial object that only has its identifier populated. Otherwise a proxy is returned that automatically loads itself on first access.

Parameters

string $documentName
$identifier

getPartialReference(string $documentName, $identifier)

Gets a partial reference to the document identified by the given type and identifier without actually loading it, if the document is not yet loaded.

The returned reference may be a partial object if the document is not yet loaded/managed. If it is a partial object it will not initialize the rest of the document state on access. Thus you can only ever safely access the identifier of a document obtained through this method.

The use-cases for partial references involve maintaining bidirectional associations without loading one side of the association or to update a document without loading it. Note, however, that in the latter case the original (persistent) document data will never be visible to the application (especially not event listeners) as it will never be loaded in the first place.

Parameters

string $documentName
$identifier

object find(string $documentName, mixed $identifier, int $lockMode = LockMode::NONE, int $lockVersion = null)

Finds a Document by its identifier.

This is just a convenient shortcut for getRepository($documentName)->find($id).

Parameters

string $documentName
mixed $identifier
int $lockMode
int $lockVersion

Return Value

object $document

clear(string|null $documentName = null)

Clears the DocumentManager.

All documents that are currently managed by this DocumentManager become detached.

Parameters

string|null $documentName if given, only documents of this type will get detached

close()

Closes the DocumentManager. All documents that are currently managed by this DocumentManager become detached. The DocumentManager may no longer be used after it is closed.

bool contains(object $document)

Determines whether a document instance is managed in this DocumentManager.

Parameters

object $document

Return Value

bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise.

Exceptions

InvalidArgumentException When the $document param is not an object.

getConfiguration()

Gets the Configuration used by the DocumentManager.

mixed createReference(object $document, array $referenceMapping)

Returns a reference to the supplied document.

Parameters

object $document
array $referenceMapping

Return Value

mixed The reference for the document in question, according to the desired mapping

Exceptions

MappingException
RuntimeException

isOpen()

Check if the Document manager is open or closed.

getFilterCollection()

Gets the filter collection.