PHP Class lithium\data\source\MongoDb

Rather than operating on records and record sets, queries against MongoDB will return nested sets of Document objects. A Document's fields can contain both simple and complex data types (i.e. arrays) including other Document objects. After installing MongoDB, you can connect to it as follows: {{{//app/config/bootstrap/connections.php: Connections::add('default', array('type' => 'MongoDb', 'database' => 'myDb'));}}} By default, it will attempt to connect to a Mongo instance running on localhost on port 27017. See __construct() for details on the accepted configuration settings.
See also: lithium\data\entity\Document
See also: lithium\data\Connections::add()
See also: lithium\data\source\MongoDb::__construct()
Inheritance: extends lithium\data\Source
Mostrar archivo Open project: unionofrad/lithium Class Usage Examples

Public Properties

Property Type Description
$connection object The MongoDB object instance.
$server object The Mongo class instance.

Protected Properties

Property Type Description
$_autoConfig array List of configuration keys which will be automatically assigned to their corresponding protected class properties.
$_boolean array List of comparison operators to use when performing boolean logic in a query.
$_classes array Classes used by this class.
$_operators Map of typical SQL-like operators to their MongoDB equivalents.
$_schema Closure A closure or anonymous function which receives an instance of this class, a collection name and associated meta information, and returns an array defining the schema for an associated model, where the keys are field names, and the values are arrays defining the type information for each field. At a minimum, type arrays must contain a 'type' key. For more information on schema definitions, and an example schema callback implementation, see the $_schema property of the Model class.

Public Methods

Method Description
__call ( string $method, array $params ) : mixed A method dispatcher that allows direct calls to native methods in PHP's Mongo object. Read more here: http://php.net/manual/class.mongo.php
__construct ( array $config = [] ) : void Constructor.
__destruct ( ) : void Destructor. Ensures that the server connection is closed and resources are freed when the adapter instance is destroyed.
calculation ( string $type, mixed $query, array $options = [] ) : integer Executes calculation-related queries, such as those required for count.
conditions ( array $conditions, object $context ) : array Maps incoming conditions with their corresponding MongoDB-native operators.
configureClass ( string $class ) : Returns Configures a model class by overriding the default dependencies for 'set' and 'entity' , and sets the primary key to '_id', in keeping with Mongo's conventions.
connect ( ) : boolean Connects to the Mongo server. Matches up parameters from the constructor to create a Mongo database connection.
create ( string $query, array $options = [] ) : boolean Create new document
delete ( string $query, array $options = [] ) : boolean Delete document
describe ( mixed $collection, mixed $fields = [], array $meta = [] ) : array Gets the column 'schema' for a given MongoDB collection. Only returns a schema if the 'schema' configuration flag has been set in the constructor.
disconnect ( ) : boolean Disconnect from the Mongo server.
enabled ( string $feature = null ) : boolean With no parameter, checks to see if the mongo extension is installed. With a parameter, queries for a specific supported feature.
fields ( array $fields, object $context ) : array Return formatted identifiers for fields.
group ( string | array $group, object $context ) : array Formats group clauses for MongoDB.
limit ( mixed $limit, object $context ) : mixed Return formatted clause for limit.
name ( string $name ) : string Quotes identifiers.
order ( mixed $order, object $context ) : mixed Return formatted clause for order.
read ( string $query, array $options = [] ) : object Read from document
relationFieldName ( $type, $name ) : string Returns the field name of a relation name (camelBack).
relationship ( string $class, string $type, string $name, array $config = [] ) : array Document relationships.
respondsTo ( string $method, boolean $internal = false ) : boolean Determines if a given method can be called.
schema ( $query, $resource = null, $context = null ) : array Normally used in cases where the query is a raw string (as opposed to a Query object), to database must determine the correct column names from the result resource. Not applicable to this data source.
sources ( string $class = null ) : array Returns the list of collections in the currently-connected database.
update ( string $query, array $options = [] ) : boolean Update document

Protected Methods

Method Description
_checkConnection ( )
_conditions ( array $conditions, string $model, object $schema, object $context ) : array Protected helper method used to format conditions.
_deleteFile ( $conditions, $options = [] )
_group ( $group, $args, $options )
_init ( ) : void Initializer. Adds operator handlers which will later allow to correctly cast any values.
_isMongoCode ( $conditions )
_ok ( $result ) : boolean Parse a MongoCollection::() response and return true on success.
_operators ( $field, $operators, $schema )
_saveFile ( $data )

Method Details

__call() public method

For example (assuming this instance is stored in Connections as 'mongo'): Manually repairs a MongoDB instance Connections::get('mongo')->repairDB($db); // returns null
public __call ( string $method, array $params ) : mixed
$method string The name of native method to call. See the link above for available class methods.
$params array A list of parameters to be passed to the native method.
return mixed The return value of the native method specified in `$method`.

__construct() public method

Constructor.
See also: lithium\data\Connections::add()
See also: lithium\data\source\MongoDb::$_schema
public __construct ( array $config = [] ) : void
$config array Configuration options required to connect to the database, including: - `'database'` _string_: The name of the database to connect to. Defaults to `null`. - `'host'` _string_: The IP or machine name where Mongo is running, followed by a colon, and the port number. Defaults to `'localhost:27017'`. - `'persistent'` _mixed_: Determines a persistent connection to attach to. See the `$options` parameter of [`Mongo::__construct()`](http://php.net/mongo.construct.php) for more information. Defaults to `false`, meaning no persistent connection is made. - `'timeout'` _integer_: The number of milliseconds a connection attempt will wait before timing out and throwing an exception. Defaults to `100`. - `'schema'` _\Closure_: A closure or anonymous function which returns the schema information for a model class. See the `$_schema` property for more information. - `'gridPrefix'` _string_: The default prefix for MongoDB's `chunks` and `files` collections. Defaults to `'fs'`. - `'replicaSet'` _string_: See the documentation for `Mongo::__construct()`. Defaults to `false`. - `'readPreference'` _mixed_: May either be a single value such as Mongo::RP_NEAREST, or an array containing a read preference and a tag set such as: array(Mongo::RP_SECONDARY_PREFERRED, array('dc' => 'east) See the documentation for `Mongo::setReadPreference()`. Defaults to null. Typically, these parameters are set in `Connections::add()`, when adding the adapter to the list of active connections.
return void

__destruct() public method

Destructor. Ensures that the server connection is closed and resources are freed when the adapter instance is destroyed.
public __destruct ( ) : void
return void

_checkConnection() protected method

protected _checkConnection ( )

_conditions() protected method

Protected helper method used to format conditions.
protected _conditions ( array $conditions, string $model, object $schema, object $context ) : array
$conditions array The conditions array to be processed.
$model string The name of the model class used in the query.
$schema object The object containing the schema definition.
$context object The `Query` object.
return array Processed query conditions.

_deleteFile() protected method

protected _deleteFile ( $conditions, $options = [] )

_group() protected method

protected _group ( $group, $args, $options )

_init() protected method

Initializer. Adds operator handlers which will later allow to correctly cast any values.
See also: lithium\data\source\MongoDb::$_operators
See also: lithium\data\source\MongoDb::_operators()
protected _init ( ) : void
return void

_isMongoCode() protected method

protected _isMongoCode ( $conditions )

_ok() protected method

Parse a MongoCollection::() response and return true on success.
protected _ok ( $result ) : boolean
return boolean

_operators() protected method

protected _operators ( $field, $operators, $schema )

_saveFile() protected method

protected _saveFile ( $data )

calculation() public method

Executes calculation-related queries, such as those required for count.
public calculation ( string $type, mixed $query, array $options = [] ) : integer
$type string Only accepts `count`.
$query mixed The query to be executed.
$options array Optional arguments for the `read()` query that will be executed to obtain the calculation result.
return integer Result of the calculation.

conditions() public method

Maps incoming conditions with their corresponding MongoDB-native operators.
public conditions ( array $conditions, object $context ) : array
$conditions array Array of conditions
$context object Context with which this method was called; currently inspects the return value of `$context->type()`.
return array Transformed conditions

configureClass() public method

Configures a model class by overriding the default dependencies for 'set' and 'entity' , and sets the primary key to '_id', in keeping with Mongo's conventions.
See also: lithium\data\Model::$_meta
See also: lithium\data\Model::$_classes
public configureClass ( string $class ) : Returns
$class string The fully-namespaced model class name to be configured.
return Returns an array containing keys `'classes'` and `'meta'`, which will be merged with their respective properties in `Model`.

connect() public method

Connects to the Mongo server. Matches up parameters from the constructor to create a Mongo database connection.
See also: lithium\data\source\MongoDb::__construct()
public connect ( ) : boolean
return boolean Returns `true` the connection attempt was successful, otherwise `false`.

create() public method

Create new document
public create ( string $query, array $options = [] ) : boolean
$query string
$options array
return boolean

delete() public method

Delete document
public delete ( string $query, array $options = [] ) : boolean
$query string
$options array
return boolean

describe() public method

Gets the column 'schema' for a given MongoDB collection. Only returns a schema if the 'schema' configuration flag has been set in the constructor.
See also: lithium\data\source\MongoDb::$_schema
public describe ( mixed $collection, mixed $fields = [], array $meta = [] ) : array
$collection mixed Specifies a collection name for which the schema should be queried.
$fields mixed Any schema data pre-defined by the model.
$meta array Any meta information pre-defined in the model.
return array Returns an associative array describing the given collection's schema.

disconnect() public method

Don't call the Mongo->close() method. The driver documentation states this should not be necessary since it auto disconnects when out of scope. With version 1.2.7, when using replica sets, close() can cause a segmentation fault.
public disconnect ( ) : boolean
return boolean True

enabled() public static method

With no parameter, checks to see if the mongo extension is installed. With a parameter, queries for a specific supported feature.
public static enabled ( string $feature = null ) : boolean
$feature string Test for support for a specific feature, i.e. `"transactions"` or `"arrays"`.
return boolean Returns `true` if the particular feature (or if MongoDB) support is enabled, otherwise `false`.

fields() public method

MongoDB does nt require field identifer escaping; as a result, this method is not implemented.
public fields ( array $fields, object $context ) : array
$fields array Fields to be parsed
$context object
return array Parsed fields array

group() public method

Formats group clauses for MongoDB.
public group ( string | array $group, object $context ) : array
$group string | array The group clause.
$context object
return array Formatted `group` clause.

limit() public method

MongoDB doesn't require limit identifer formatting; as a result, this method is not implemented.
public limit ( mixed $limit, object $context ) : mixed
$limit mixed The `limit` clause to be formatted.
$context object The `Query` object instance.
return mixed Formatted `limit` clause.

name() public method

MongoDb does not need identifiers quoted, so this method simply returns the identifier.
public name ( string $name ) : string
$name string The identifier to quote.
return string The quoted identifier.

order() public method

Return formatted clause for order.
public order ( mixed $order, object $context ) : mixed
$order mixed The `order` clause to be formatted
$context object
return mixed Formatted `order` clause.

read() public method

Read from document
public read ( string $query, array $options = [] ) : object
$query string
$options array
return object

relationFieldName() public method

Returns the field name of a relation name (camelBack).
public relationFieldName ( $type, $name ) : string
return string

relationship() public method

Document relationships.
public relationship ( string $class, string $type, string $name, array $config = [] ) : array
$class string
$type string Relationship type, e.g. `belongsTo`.
$name string
$config array
return array

respondsTo() public method

Determines if a given method can be called.
public respondsTo ( string $method, boolean $internal = false ) : boolean
$method string Name of the method.
$internal boolean Provide `true` to perform check from inside the class/object. When `false` checks also for public visibility; defaults to `false`.
return boolean Returns `true` if the method can be called, `false` otherwise.

schema() public method

Normally used in cases where the query is a raw string (as opposed to a Query object), to database must determine the correct column names from the result resource. Not applicable to this data source.
public schema ( $query, $resource = null, $context = null ) : array
return array

sources() public method

Returns the list of collections in the currently-connected database.
public sources ( string $class = null ) : array
$class string The fully-name-spaced class name of the model object making the request.
return array Returns an array of objects to which models can connect.

update() public method

Update document
public update ( string $query, array $options = [] ) : boolean
$query string
$options array
return boolean

Property Details

$_autoConfig protected_oe property

List of configuration keys which will be automatically assigned to their corresponding protected class properties.
protected array $_autoConfig
return array

$_boolean protected_oe property

List of comparison operators to use when performing boolean logic in a query.
protected array $_boolean
return array

$_classes protected_oe property

Classes used by this class.
protected array $_classes
return array

$_operators protected_oe property

Map of typical SQL-like operators to their MongoDB equivalents.
protected $_operators

$_schema protected_oe property

A closure or anonymous function which receives an instance of this class, a collection name and associated meta information, and returns an array defining the schema for an associated model, where the keys are field names, and the values are arrays defining the type information for each field. At a minimum, type arrays must contain a 'type' key. For more information on schema definitions, and an example schema callback implementation, see the $_schema property of the Model class.
See also: lithium\data\Model::$_schema
protected Closure $_schema
return Closure

$connection public_oe property

The MongoDB object instance.
public object $connection
return object

$server public_oe property

The Mongo class instance.
public object $server
return object