PHP Class Mongolid\DataMapper\DataMapper

The DataMapper will always use a Schema trough the SchemaMapper to parse the document in and out of the database.
Inheritance: implements Mongolid\Schema\HasSchemaInterface
Show file Open project: leroy-merlin-br/mongolid Class Usage Examples

Public Properties

Property Type Description
$schemaClass string Name of the schema class to be used.

Protected Properties

Property Type Description
$assembler EntityAssembler Have the responsibility of assembling the data coming from the database into actual entities.
$connPool Mongolid\Connection\Pool Connections that are going to be used to interact with the database.
$eventService Mongolid\Event\EventTriggerService In order to dispatch events when necessary.
$schema Mongolid\Schema\Schema Schema object. Will be set after the $schemaClass.

Public Methods

Method Description
__construct ( Pool $connPool )
all ( ) : Cursor Retrieve a database cursor that will return all documents as $this->schema->entityClass objects upon iteration.
delete ( mixed $entity, array $options = [] ) : boolean Removes the given document from the collection.
first ( mixed $query = [], array $projection = [], boolean $cacheable = false ) : mixed Retrieve one $this->schema->entityClass objects that matches the given query.
firstOrFail ( mixed $query = [], array $projection = [], boolean $cacheable = false ) : mixed Retrieve one $this->schema->entityClass objects that matches the given query. If no document was found, throws ModelNotFoundException.
getSchema ( ) : Schema
insert ( mixed $entity, array $options = [], boolean $fireEvents = true ) : boolean Inserts the given object into database. Returns success if write concern is acknowledged. Since it's an insert, it will fail if the _id already exists.
save ( mixed $entity, array $options = [] ) : boolean Upserts the given object into database. Returns success if write concern is acknowledged.
setSchema ( Schema $schema ) Set a Schema object that describes an Entity in MongoDB.
update ( mixed $entity, array $options = [] ) : boolean Updates the given object into database. Returns success if write concern is acknowledged. Since it's an update, it will fail if the document with the given _id did not exists.
where ( mixed $query = [], array $projection = [], boolean $cacheable = false ) : Cursor Retrieve a database cursor that will return $this->schema->entityClass objects that upon iteration.

Protected Methods

Method Description
fireEvent ( string $event, mixed $entity, boolean $halt = false ) : mixed Triggers an event. May return if that event had success.
getAssembler ( ) : EntityAssembler Retrieves an EntityAssembler instance.
getCollection ( ) : MongoDB\Collection Retrieves the Collection object.
getSchemaMapper ( ) : SchemaMapper Returns a SchemaMapper with the $schema or $schemaClass instance.
parseToDocument ( mixed $entity ) : array Parses an object with SchemaMapper and the given Schema.
prepareArrayFieldOfQuery ( array $value ) : array Prepares an embedded array of an query. It will convert string ObjectIDs in operators into actual objects.
prepareProjection ( array $fields ) : array Converts the given projection fields to Mongo driver format.
prepareValueQuery ( mixed $value ) : array Transforms a value that is not an array into an MongoDB query (array).

Private Methods

Method Description
afterSuccess ( mixed $entity ) Perform actions on object before firing the after event.
mergeOptions ( array $defaultOptions = [], array $toMergeOptions = [] ) : array Merge all options.

Method Details

__construct() public method

public __construct ( Pool $connPool )
$connPool Mongolid\Connection\Pool The connections that are going to be used to interact with the database.

all() public method

Retrieve a database cursor that will return all documents as $this->schema->entityClass objects upon iteration.
public all ( ) : Cursor
return Mongolid\Cursor\Cursor

delete() public method

Notice: Deletes with Unacknowledged WriteConcern will not fire deleted event.
public delete ( mixed $entity, array $options = [] ) : boolean
$entity mixed The entity used in the operation.
$options array Possible options to send to mongo driver.
return boolean Success (but always false if write concern is Unacknowledged)

fireEvent() protected method

Triggers an event. May return if that event had success.
protected fireEvent ( string $event, mixed $entity, boolean $halt = false ) : mixed
$event string Identification of the event.
$entity mixed Event payload.
$halt boolean True if the return of the event handler will be used in a conditional.
return mixed Event handler return.

first() public method

Retrieve one $this->schema->entityClass objects that matches the given query.
public first ( mixed $query = [], array $projection = [], boolean $cacheable = false ) : mixed
$query mixed MongoDB query to retrieve the document.
$projection array Fields to project in Mongo query.
$cacheable boolean Retrieves the first through a CacheableCursor.
return mixed First document matching query as an $this->schema->entityClass object

firstOrFail() public method

Retrieve one $this->schema->entityClass objects that matches the given query. If no document was found, throws ModelNotFoundException.
public firstOrFail ( mixed $query = [], array $projection = [], boolean $cacheable = false ) : mixed
$query mixed MongoDB query to retrieve the document.
$projection array Fields to project in Mongo query.
$cacheable boolean Retrieves the first through a CacheableCursor.
return mixed First document matching query as an $this->schema->entityClass object

getAssembler() protected method

Retrieves an EntityAssembler instance.
protected getAssembler ( ) : EntityAssembler
return EntityAssembler

getCollection() protected method

Retrieves the Collection object.
protected getCollection ( ) : MongoDB\Collection
return MongoDB\Collection

getSchema() public method

public getSchema ( ) : Schema
return Mongolid\Schema\Schema

getSchemaMapper() protected method

Returns a SchemaMapper with the $schema or $schemaClass instance.
protected getSchemaMapper ( ) : SchemaMapper
return SchemaMapper

insert() public method

Notice: Inserts with Unacknowledged WriteConcern will not fire inserted event.
public insert ( mixed $entity, array $options = [], boolean $fireEvents = true ) : boolean
$entity mixed The entity used in the operation.
$options array Possible options to send to mongo driver.
$fireEvents boolean Whether events should be fired.
return boolean Success (but always false if write concern is Unacknowledged)

parseToDocument() protected method

Parses an object with SchemaMapper and the given Schema.
protected parseToDocument ( mixed $entity ) : array
$entity mixed The object to be parsed.
return array Document

prepareArrayFieldOfQuery() protected method

Prepares an embedded array of an query. It will convert string ObjectIDs in operators into actual objects.
protected prepareArrayFieldOfQuery ( array $value ) : array
$value array Array that will be treated.
return array Prepared array.

prepareProjection() protected method

How to use: As Mongo projection using boolean values: From: ['name' => true, '_id' => false] To: ['name' => true, '_id' => false] As Mongo projection using integer values From: ['name' => 1, '_id' => -1] To: ['name' => true, '_id' => false] As an array of string: From: ['name', '_id'] To: ['name' => true, '_id' => true] As an array of string to exclude some fields: From: ['name', '-_id'] To: ['name' => true, '_id' => false]
protected prepareProjection ( array $fields ) : array
$fields array Fields to project.
return array

prepareValueQuery() protected method

This method will take care of converting a single value into a query for an _id, including when a objectId is passed as a string.
protected prepareValueQuery ( mixed $value ) : array
$value mixed The _id of the document.
return array Query for the given _id

save() public method

Notice: Saves with Unacknowledged WriteConcern will not fire saved event.
public save ( mixed $entity, array $options = [] ) : boolean
$entity mixed The entity used in the operation.
$options array Possible options to send to mongo driver.
return boolean Success (but always false if write concern is Unacknowledged)

setSchema() public method

Set a Schema object that describes an Entity in MongoDB.
public setSchema ( Schema $schema )
$schema Mongolid\Schema\Schema

update() public method

Notice: Updates with Unacknowledged WriteConcern will not fire updated event.
public update ( mixed $entity, array $options = [] ) : boolean
$entity mixed The entity used in the operation.
$options array Possible options to send to mongo driver.
return boolean Success (but always false if write concern is Unacknowledged)

where() public method

Retrieve a database cursor that will return $this->schema->entityClass objects that upon iteration.
public where ( mixed $query = [], array $projection = [], boolean $cacheable = false ) : Cursor
$query mixed MongoDB query to retrieve documents.
$projection array Fields to project in Mongo query.
$cacheable boolean Retrieves a CacheableCursor instead.
return Mongolid\Cursor\Cursor

Property Details

$assembler protected property

Have the responsibility of assembling the data coming from the database into actual entities.
protected EntityAssembler,Mongolid\DataMapper $assembler
return EntityAssembler

$connPool protected property

Connections that are going to be used to interact with the database.
protected Pool,Mongolid\Connection $connPool
return Mongolid\Connection\Pool

$eventService protected property

In order to dispatch events when necessary.
protected EventTriggerService,Mongolid\Event $eventService
return Mongolid\Event\EventTriggerService

$schema protected property

Schema object. Will be set after the $schemaClass.
protected Schema,Mongolid\Schema $schema
return Mongolid\Schema\Schema

$schemaClass public property

Name of the schema class to be used.
public string $schemaClass
return string