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);

Methods

__construct(Connection $conn = null, Configuration $config = null, EventManager $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 DocumentManager
create(Connection $conn = null, Configuration $config = null, EventManager $eventManager = null)

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

EventManager
getEventManager()

Gets the EventManager used by the DocumentManager.

Connection
getConnection()

Gets the PHP Mongo 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.

getClassMetadata(string $className)

Returns the metadata for a class.

Database
getDocumentDatabase(string $className)

Returns the MongoDB instance for a class.

array
getDocumentDatabases()

Gets the array of instantiated document database instances.

Collection
getDocumentCollection(string $className)

Returns the MongoCollection instance for a class.

array
getDocumentCollections()

Gets the array of instantiated document collection instances.

createQueryBuilder(string $documentName = null)

Create a new Query 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 $lockVersion = null)

Acquire a lock on the given document.

unlock(object $document)

Releases a lock on the given document.

getRepository(string $documentName)

Gets the repository for a document class.

flush(object $document = null, array $options = array())

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

mixed|object
getReference(string $documentName, string|object $identifier)

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

object
getPartialReference(string $documentName, mixed $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.

boolean
contains(object $document)

Determines whether a document instance is managed in this DocumentManager.

getConfiguration()

Gets the Configuration used by the DocumentManager.

array
createDBRef(mixed $document, array $referenceMapping = null)

Returns a DBRef array for the supplied document.

bool
isOpen()

Check if the Document manager is open or closed.

getFilterCollection()

Gets the filter collection.

Details

protected __construct(Connection $conn = null, Configuration $config = null, EventManager $eventManager = null)

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

Parameters

Connection $conn
Configuration $config
EventManager $eventManager

ProxyFactory getProxyFactory()

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

Return Value

ProxyFactory

static DocumentManager create(Connection $conn = null, Configuration $config = null, EventManager $eventManager = null)

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

Parameters

Connection $conn
Configuration $config
EventManager $eventManager

Return Value

DocumentManager

EventManager getEventManager()

Gets the EventManager used by the DocumentManager.

Return Value

EventManager

Connection getConnection()

Gets the PHP Mongo instance that this DocumentManager wraps.

Return Value

Connection

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

UnitOfWork getUnitOfWork()

Gets the UnitOfWork used by the DocumentManager to coordinate operations.

Return Value

UnitOfWork

HydratorInterface getHydratorFactory()

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

Return Value

HydratorInterface

SchemaManager getSchemaManager()

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

Return Value

SchemaManager

ClassMetadata getClassMetadata(string $className)

Returns the metadata for a class.

Parameters

string $className The class name.

Return Value

ClassMetadata

Database getDocumentDatabase(string $className)

Returns the MongoDB instance for a class.

Parameters

string $className The class name.

Return Value

Database

array getDocumentDatabases()

Gets the array of instantiated document database instances.

Return Value

array

Collection getDocumentCollection(string $className)

Returns the MongoCollection instance for a class.

Parameters

string $className The class name.

Return Value

Collection

Exceptions

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

array getDocumentCollections()

Gets the array of instantiated document collection instances.

Return Value

array

Builder createQueryBuilder(string $documentName = null)

Create a new Query instance for a class.

Parameters

string $documentName The document class name.

Return Value

Builder

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 $lockVersion = null)

Acquire a lock on the given document.

Parameters

object $document
int $lockMode
int $lockVersion

Exceptions

InvalidArgumentException

unlock(object $document)

Releases a lock on the given document.

Parameters

object $document

Exceptions

InvalidArgumentException if the $document param is not an object

DocumentRepository getRepository(string $documentName)

Gets the repository for a document class.

Parameters

string $documentName The name of the Document.

Return Value

DocumentRepository The repository.

flush(object $document = null, array $options = array())

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

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

Exceptions

InvalidArgumentException

mixed|object getReference(string $documentName, string|object $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
string|object $identifier

Return Value

mixed|object The document reference.

object getPartialReference(string $documentName, mixed $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 The name of the document type.
mixed $identifier The document identifier.

Return Value

object The (partial) document reference.

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.

boolean contains(object $document)

Determines whether a document instance is managed in this DocumentManager.

Parameters

object $document

Return Value

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

Exceptions

InvalidArgumentException When the $document param is not an object

Configuration getConfiguration()

Gets the Configuration used by the DocumentManager.

Return Value

Configuration

array createDBRef(mixed $document, array $referenceMapping = null)

Returns a DBRef array for the supplied document.

Parameters

mixed $document A document object
array $referenceMapping Mapping for the field that references the document

Return Value

array A DBRef array

Exceptions

InvalidArgumentException

bool isOpen()

Check if the Document manager is open or closed.

Return Value

bool

FilterCollection getFilterCollection()

Gets the filter collection.

Return Value

FilterCollection The active filter collection.