AbstractMongoDao<T>
Abstract MongoDB DAO Service
abstract class AbstractMongoDao<T> extends MongoDb implements EntityRepo<T>, DbRefResolver<T>, PublisherAwareuse MongoHelperuse Publishing- Copyright
- 2015-2016 Appertly
- License
- Apache-2.0
Constructors / Destructors
| Returns | Signature and Description |
|---|---|
void | __construct(, , )Creates a new AbstractMongoDao. |
Instance Methods
| Returns | Signature and Description |
|---|---|
int | countAll()Counts several records by some arbitrary criteria. |
Iterator<T> | findAll(, , )Finds several records by some arbitrary criteria. |
?T | findById()Gets a single document by ID. |
?T | findOne()Finds a single record by some arbitrary criteria. |
T | get()Gets a single document by ID, throwing an exception if it's not found. |
Traversable<T> | getAll()Gets several documents by ID. |
ImmMap<string,T> | getInstanceMap()Gets a Map that relates identifier to instance |
string | getType()Gets the type of entity produced, mainly for ACL reasons. |
bool | isResolvable()Gets whether or not this class supports the reference type. |
?T | Resolves a MongoDB DbRef. |
Traversable<T> | resolveAll()Resolves a MongoDB DbRef. |
| Returns | Signature and Description |
|---|---|
WriteResult | doCreate()Creates a record. |
WriteResult | doDelete()Deletes a record. |
WriteResult | doPersist()Creates a record using a MongoDB |
WriteResult | doUpdate(, , )Updates a record. |
?WriteResult | doUpdateModifiable(, )Updates a record. |
Ta | Makes sure a document isn't null. |
?T | getFromCache()Gets an entry from the cache |
?T | maybeCache()Possibly add the entity to the cache. |
Traversable<T> | maybeCacheAll()Possibly add entities to the cache. |
UTCDateTime | now()Gets the current time. |
?UTCDateTime | toDate()Tries to parse a date. |
ObjectID | toId()Transforms a literal into a MongoDB ObjectId. |
ConstVector<ObjectID> | toIds()Transforms literals into MongoDB ObjectIds. |
Method Details
__construct
__construct(Manager $manager, string $collection, ?ConstMap<string,mixed> $options = null)Creates a new AbstractMongoDao.
Current accepted configuration values:
versioned– Whether to enforce optimistic locking via a version field (default: true)caching– Whether to cache entities by ID (default: true)typeMapRoot– The type used to unserialize BSON root documentstypeMapDocument– The type used to unserialize BSON nested documentsreadPreference– Must be aMongoDB\Driver\ReadPreferencewriteConcern– Must be aMongoDB\Driver\WriteConcern
As for the typeMap options, you can see
Deserialization from BSON
for more information.
| Name | Type | Description |
|---|---|---|
$manager | Manager | The MongoDB Manager |
$collection | string | The collection to wrap |
$options | ?ConstMap<string,mixed> | Map of configuration values |
countAll
countAll(ConstMap<string,mixed> $criteria)Counts several records by some arbitrary criteria.
- Since
- 0.6.0
| Name | Type | Description |
|---|---|---|
$criteria | ConstMap<string,mixed> | Field to value pairs |
| Type | Description |
|---|---|
int | The count of the documents |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the result cannot be returned |
Generic | If any other database problem occurs |
doCreate
doCreate(ConstMap<string,mixed> $record)Creates a record.
| Name | Type | Description |
|---|---|---|
$record | ConstMap<string,mixed> | The record to insert, ready to go |
| Type | Description |
|---|---|
WriteResult | Whatever MongoDB returns |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Violating | If a constraint is violated |
Generic | If any other database problem occurs |
doDelete
doDelete(mixed $id)Deletes a record.
| Name | Type | Description |
|---|---|---|
$id | ObjectID | string | The document identifier, either a string or |
| Type | Description |
|---|---|
WriteResult | Whatever MongoDB returns |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the document doesn't exist |
Generic | If any other database problem occurs |
doPersist
doPersist(Persistable $record)Creates a record using a MongoDB Persistable.
- Since
- 0.3.0
| Name | Type | Description |
|---|---|---|
$record | Persistable | The document to insert, ready to go |
| Type | Description |
|---|---|
WriteResult | Whatever MongoDB returns |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Violating | If a constraint is violated |
Generic | If any other database problem occurs |
doUpdate
doUpdate(mixed $id, ConstMap<string,Map<string,mixed>> $operations, ?int $version = null)Updates a record.
| Name | Type | Description |
|---|---|---|
$id | ObjectID | string | The document identifier, either a string or |
$operations | ConstMap<string,Map<string,mixed>> | The operations to send to MongoDB |
$version | ?int | Optional version for optimistic lock checking |
| Type | Description |
|---|---|
WriteResult | Whatever MongoDB returns |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the document doesn't exist |
Conflicting | If optimistic/pessimistic lock fails |
Violating | If a constraint is violated |
Generic | If any other database problem occurs |
doUpdateModifiable
doUpdateModifiable(Modifiable $entity, ?int $version = null)Updates a record.
- Since
- 0.5.1
| Name | Type | Description |
|---|---|---|
$entity | Modifiable | The entity to update |
$version | ?int | Optional version for optimistic lock checking |
| Type | Description |
|---|---|
?WriteResult | Whatever MongoDB returns |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Conflicting | If optimistic/pessimistic lock fails |
Violating | If a constraint is violated |
Generic | If any other database problem occurs |
ensure
Inherited from MongoHelper
ensure<Ta>(mixed $id, ?Ta $document)Makes sure a document isn't null.
| Name | Type | Description |
|---|---|---|
$id | mixed | The document identifier, either a |
$document | ?Ta | The document to check |
| Type | Description |
|---|---|
Ta | Returns |
| Type | Conditions |
|---|---|
Unretrievable | if the document is null |
findAll
findAll(ConstMap<string,mixed> $criteria, ?Pagination $pagination = null, ?bool $totalCount = false)Finds several records by some arbitrary criteria.
| Name | Type | Description |
|---|---|---|
$criteria | ConstMap<string,mixed> | Field to value pairs |
$pagination | ?Pagination | Optional pagination parameters |
$totalCount | ?bool | Return a |
| Type | Description |
|---|---|
Iterator<T> | The objects found or null if none |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the result cannot be returned |
Generic | If any other database problem occurs |
findById
findById(mixed $id)Gets a single document by ID.
| Name | Type | Description |
|---|---|---|
$id | mixed | The document identifier |
| Type | Description |
|---|---|
?T | The entity |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the result cannot be returned |
Generic | If any other database problem occurs |
findOne
findOne(ConstMap<string,mixed> $criteria)Finds a single record by some arbitrary criteria.
| Name | Type | Description |
|---|---|---|
$criteria | ConstMap<string,mixed> | Field to value pairs |
| Type | Description |
|---|---|
?T | The object found or null if none |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the result cannot be returned |
Generic | If any other database problem occurs |
get
get(mixed $id)Gets a single document by ID, throwing an exception if it's not found.
| Name | Type | Description |
|---|---|---|
$id | mixed | The document identifier |
| Type | Description |
|---|---|
T | The entity |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the document doesn't exist |
Generic | If any other database problem occurs |
getAll
getAll(ConstVector<mixed> $ids)Gets several documents by ID.
| Name | Type | Description |
|---|---|---|
$ids | ConstVector<mixed> | Array of identifiers |
| Type | Description |
|---|---|
Traversable<T> | The results |
| Type | Conditions |
|---|---|
Unreachable | If the connection fails |
Unretrievable | If the result cannot be returned |
Generic | If any other database problem occurs |
getFromCache
getFromCache(string $id)Gets an entry from the cache
| Name | Type | Description |
|---|---|---|
$id | string | The cache key |
| Type | Description |
|---|---|
?T | The entity found or null |
getInstanceMap
getInstanceMap(Traversable<T> $entities)Gets a Map that relates identifier to instance
| Name | Type | Description |
|---|---|---|
$entities | Traversable<T> | The entities to "zip" |
| Type | Description |
|---|---|
ImmMap<string,T> | The instances keyed by identifier |
getType
Inherited from EntityRepo
getType()Gets the type of entity produced, mainly for ACL reasons.
| Type | Description |
|---|---|
string | The entity type |
isResolvable
isResolvable(string $ref)Gets whether or not this class supports the reference type.
| Name | Type | Description |
|---|---|---|
$ref | string | The reference type (usually a MongoDB collection name) |
| Type | Description |
|---|---|
bool |
|
maybeCache
maybeCache(?T $entity)Possibly add the entity to the cache.
| Name | Type | Description |
|---|---|---|
$entity | ?T | The entity to possibly cache |
| Type | Description |
|---|---|
?T | The same entity that came in |
maybeCacheAll
maybeCacheAll(Iterator<T> $entities)Possibly add entities to the cache.
| Name | Type | Description |
|---|---|---|
$entities | Iterator<T> | The entities to possibly cache |
| Type | Description |
|---|---|
Traversable<T> | The same entities that came in |
now
Inherited from MongoHelper
now()Gets the current time.
| Type | Description |
|---|---|
UTCDateTime | The current time |
resolve
resolve(DbRef $ref)Resolves a MongoDB DbRef.
| Name | Type | Description |
|---|---|---|
$ref | DbRef | The DbRef to load |
| Type | Description |
|---|---|
?T | The loaded entity or |
| Type | Conditions |
|---|---|
InvalidArgumentException | If |
Unreachable | If the connection fails |
Unretrievable | If the result cannot be retrieved |
Generic | If any other database problem occurs |
resolveAll
resolveAll(Traversable<DbRef> $refs)Resolves a MongoDB DbRef.
| Name | Type | Description |
|---|---|---|
$refs | Traversable<DbRef> | The DbRefs to load |
| Type | Description |
|---|---|
Traversable<T> | The loaded entities |
| Type | Conditions |
|---|---|
InvalidArgumentException | If any |
Unreachable | If the connection fails |
Unretrievable | If the result cannot be retrieved |
Generic | If any other database problem occurs |
toDate
Inherited from MongoHelper
toDate(mixed $date)Tries to parse a date.
| Name | Type | Description |
|---|---|---|
$date | mixed | The possible string date value, a string, a |
| Type | Description |
|---|---|
?UTCDateTime | The MongoDB datetime or null |
toId
Inherited from MongoHelper
toId(mixed $id)Transforms a literal into a MongoDB ObjectId.
| Name | Type | Description |
|---|---|---|
$id | mixed | If it's an |
| Type | Description |
|---|---|
ObjectID | The ObjectID |
toIds
Inherited from MongoHelper
toIds(ConstVector<mixed> $ids)Transforms literals into MongoDB ObjectIds.
| Name | Type | Description |
|---|---|---|
$ids | ConstVector<mixed> | Goes through each entry, converts to |
| Type | Description |
|---|---|
ConstVector<ObjectID> | The ObjectIDs |