2017 © Pedro Peláez
 

cakephp-plugin cakephp-monga

CakeMonga plugin for CakePHP

image

lewestopher/cakephp-monga

CakeMonga plugin for CakePHP

  • Thursday, October 19, 2017
  • by lewestopher
  • Repository
  • 4 Watchers
  • 13 Stars
  • 502 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 17 Versions
  • 6 % Grown

The README.md

cakephp-monga

Framework Database license Github All Releases Travis Coverage Status, (*1)

A plugin for accessing MongoDB NoSQL data stores in CakePHP 3.x., (*2)

Requirements

  • Composer
  • CakePHP 3.x
  • PHP 5.4+
  • MongoDB
  • Pecl Mongo extension

Installation

In your CakePHP root directory: run the following command:, (*3)

composer require lewestopher/cakephp-monga

Then in your config/bootstrap.php in your project root, add the following snippet:, (*4)

// In project_root/config/bootstrap.php:

Plugin::load('CakeMonga');

or you can use the following shell command to enable to plugin in your bootstrap.php automatically:, (*5)

bin/cake plugin load CakeMonga

Extended Documentation

For the extended docs, please visit our Wiki., (*6)

Usage

First, we define a new Datasource in our config/app.php file with our namespaced Connection class name:, (*7)

// In project_root/config/app.php:

'Datasources' => [

    'default' => [
        // ... Default SQL Datasource
    ],

    'mongo_db' => [
        'className' => 'CakeMonga\Database\MongoConnection',
    ]
],

Then we can instantiate our MongoDB connection anywhere that we need in the application via the ConnectionManager class:, (*8)

class ExampleController extends Controller
{
    public function index()
    {
        $cake_monga = ConnectionManager::get('mongo_db');
    }
}

Then from there we can get our Monga instance by using the connect() method on the returned connection:, (*9)

$cake_monga = ConnectionManager::get('mongo_db');
$mongodb = $cake_monga->connect(); // An instance of the Monga Connection object
$database_list = $mongodb->listDatabases(); // We can call all of the methods on that Monga object provided by their API

Note that the $mongodb object instantiated above with the connect() method is the same object returned by Monga::connection() in the Monga API:, (*10)

$cake_monga = ConnectionManager::get('mongo_db');
$mongodb = $cake_monga->connect();

// Alternatively:

$mongodb = Monga::connection($dns, $config_opts);

This information should help you make the bridge between instantiating the Datasource using CakePHP and utilizing the Monga API for data retrieval and saving., (*11)

Configuration

cakephp-monga accepts all of the same options in the Datasource configuration that can be passed into the MongoClient() object in PHP. Documentation for these options is defined here., (*12)

// In project_root/config/app.php: 

'Datasources' => [

    'default' => [
        // ... Default SQL Datasource
    ],

    'mongo_db' => [
        'className' => 'CakeMonga\Database\MongoConnection',
        'logger' => null,
        'authMechanism' => null,
        'authSource' => null,
        'connect' => true,
        'connectTimeoutMS' => 60000,
        'db' => null,
        'dns' => 'mongodb://localhost:27017',
        'fsync' => null,
        'journal' => null,
        'gssapiServiceName' => 'mongodb',
        'username' => null,
        'password' => null,
        'readPreference' => null,
        'readPreferenceTags' => null,
        'replicaSet' => null,
        'secondaryAcceptableLatencyMS' => 15,
        'socketTimeoutMS' => 30000,
        'ssl' => false,
        'w' => 1,
        'wTimeoutMS' => 10000
    ]
],

Connecting to a custom DNS using this library

By default, this library connects to the mongodb://localhost:27017 DNS string. You can specify a custom DNS to connect on by setting a 'dns' key on the connection's Datasource hash in the config/app.php file:, (*13)

// In project_root/config/app.php:

'Datasources' => [

    'mongo_db' => [
        'className' => 'CakeMonga\Database\MongoConnection',
        'dns' => 'mongodb://your.remote.host:27017'
    ]
],

API and Accessing your MongoDB Instance

This plugin is a wrapper of the Mongo plugin by the League of Extraordinary Packages. To find out how to query, save, and update data within your Mongo collections, check out the Monga documentation., (*14)

Defining a custom Collection class

View the Accessing Collections Extended Docs page for more information on creating Collection classes., (*15)

As of version 0.2.0, CakeMonga supports the usage of custom Collection classes. These custom classes are located with the src/Model/MongoCollection folder an extend the CakeMonga\MongoCollection\BaseCollection class. Now you can use these classes to encapsulate data layer logic into the appropriate class locations. These methods are direct abstractions of their Monga counterparts. You can view the docs for these methods on the Monga API Docs. The BaseCollection class provides the following methods for data access:, (*16)

class BaseCollection {
    public function getCollection();
    public function find($query = [], $fields = [], $findOne = false);
    public function findOne($query = [], $fields = []);
    public function drop();
    public function listIndexes();
    public function save($document, $options = []);
    public function update($values = [], $query = null, $options = []);
    public function insert(array $data, $options = []);
    public function remove($criteria, $options = []);
    public function truncate();
    public function aggregate($aggregation = []);
    public function distinct($key, $query = []);
    public function count($query = []);
}
````

Note that the MongoDB collection that is utilized by custom Collection classes is the tableized name of the Collection class itself.  For example, 
`UsersCollection.php` would map to the `users` collection inside of your MongoDB instance.  

### Retrieving a custom Collection model using CollectionRegistry

As of 0.2.0, custom Collection models extended from `BaseCollection` can be retrieved using the `CollectionRegistry` singleton
with the same syntax that `TableRegistry` employs:

```php
// Define a custom User collection at src/Model/MongoCollection/UserCollection.php. 
use CakeMonga\MongoCollection\BaseCollection;

class UsersCollection extends BaseCollection
{
    public function getUsersNamedJohn()
    {
        return $this->find(['name' => 'John']);
    }
}

// We can retrieve this UsersCollection by using the static ::get() method on CollectionRegistry
use CakeMonga\MongoCollection\CollectionRegistry;

$users_collection = CollectionRegistry::get('Users');

By default, the CollectionRegistry utilizes the default connection defined as 'mongo_db' in your app.php file. Want to use a different Mongo datasource? No problem, just pass in a datasource string to the 'connection' attribute of the config array for CollectionRegistry::get():, (*17)

$users_collection = CollectionRegistry::get('Users', [
    'connection' => 'secondary_mongo_datasource'
]

This would construct the UsersCollection class with a connection to the other datasource., (*18)

Collection Event Hooks

As of 0.4.0, you can now define the following events on your Collection classes:, (*19)

use CakeMonga\MongoCollection\BaseCollection;

class CustomCollection extends BaseCollection
{
    public function beforeFind($event, $query, $fields, $findOne);
    public function beforeSave($event, $document);
    public function afterSave($event, $document)
    public function beforeInsert($event, $data);
    public function afterInsert($event, $results)
    public function beforeUpdate($event, $values, $query)
    public function afterUpdate($event, $document);
    public function beforeRemove($event, $criteria);
    public function afterRemove($event, $result, $criteria);
}

You can find more information on Collection events on the Accessing Collections Wiki Page, (*20)

Custom Behaviors

As of 0.5.0, you can attach Behaviors to classes extending the BaseCollection class in the same manner that you would to a Table object. To create a custom behavior for a BaseCollection, you should extend the MongoBehavior class instead of the regular Behavior class., (*21)

You can find out more about Behaviors on collections on the Accessing Collections Wiki Page., (*22)

Query Logging

As of version 0.3.0, CakeMonga supports query logging via the Mongo logging context. To learn how to enable logging and create custom loggers, visit the Query Logging Wiki Page., (*23)

What is cakephp-monga?

This plugin is a wrapper for the popular Monga library provided by The League of Extraordinary packages. In it's current form, this plugin is intended to get you quickly set up and running with access to a MongoDB instance so that you can access your data in your application. This plugin provides all of the same functionality that the Monga library provides in terms of building queries and retrieving your data., (*24)

What is cakephp-monga not?

This plugin is not currently intended as a drop in replacement for the SQL ORM provided by CakePHP core. While you could theoretically build an entire application using cakephp-monga as the data layer, this plugin does not have the kind of application level integration (Events, Logging, etc) that the current ORM does. Additionally, there is not abstraction layer for Database level, Collection level, or Entity level objects (EG - Defining methods on a supposed UserCollection.php, or creating a Mongo Entity at User.php), although this is on the roadmap for a future version very soon., (*25)

Additionally, it's important to recognize that while certain relational features can be emulated within a MongoDB dataset, Mongo is still not an ACID compliant database. In the future, Collection level classes will be built for object abstraction that will implement CakePHP's Repository interface, but it should be noted that full ORM features will not be supported as Mongo is not a true object relational database., (*26)

Roadmap

Here are some of the features that I plan on integrating into this project very soon:, (*27)

  • [X] Basic Connection object support for retrieving an instance of the Monga class for simple data retrieval. Added in 0.1.0
  • [X] Collection ~~and Entity level~~ abstraction layers (EG - UserCollection.php ~~and User.php~~ for Mongo) Added in 0.2.0
  • [ ] SSL Support via the stream context on the third argument of the MongoClient constructor
  • [X] Query logging via the stream context on the third argument of the MongoClient constructor Added in 0.3.0
  • [X] A CollectionRegistry class for retrieving Mongo collections with connection params already passed in. Added in 0.2.0
  • [X] Custom behavior support on the Collection level class Added in 0.5.0
  • [X] Events integration on the Collection level class Added in 0.4.0
  • [ ] Validation Support

Support

For bugs and feature requests, please use the issues section of this repository., (*28)

Contributing

To contribute to this plugin please follow a few basic rules., (*29)

Change Log

Yes, we have one of those., (*30)

Creators

Wes King, (*31)

Frank de Jonge - Creator of the Monga Dependency, (*32)

Monga Contributors, (*33)

License

Copyright 2016, Wes King, (*34)

Licensed under The MIT License Redistributions of files must retain the above copyright notice., (*35)

The Versions

19/10 2017

dev-0.5.0-dev

dev-0.5.0-dev

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

07/04 2017

dev-0.6.0-dev

dev-0.6.0-dev

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

04/11 2016

dev-master

9999999-dev

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

04/11 2016

dev-0.5.0-stable

dev-0.5.0-stable

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

04/11 2016

0.5.0

0.5.0.0

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

27/10 2016

dev-0.4.0-stable

dev-0.4.0-stable

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

27/10 2016

0.4.0

0.4.0.0

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

27/10 2016

dev-0.4.0-dev

dev-0.4.0-dev

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2016

dev-0.3.0-dev

dev-0.3.0-dev

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2016

dev-0.3.0-stable

dev-0.3.0-stable

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2016

0.3.0

0.3.0.0

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

15/09 2016

dev-query-0.2.x-edge

dev-query-0.2.x-edge

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

13/09 2016

dev-0.2.0-stable

dev-0.2.0-stable

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

12/09 2016

dev-0.2.0-dev

dev-0.2.0-dev

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

12/09 2016

0.2.0

0.2.0.0

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

13/08 2016

dev-0.1.0-stable

dev-0.1.0-stable

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires

12/08 2016

0.1.0

0.1.0.0

CakeMonga plugin for CakePHP

  Sources   Download

MIT

The Requires

 

The Development Requires