PHPackages                             dilab/cake-mongo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. dilab/cake-mongo

ActiveCakephp-plugin[Database &amp; ORM](/categories/database)

dilab/cake-mongo
================

A MongoDB datasource and data mapper for CakePHP 3.3

455211[7 issues](https://github.com/dilab/cake-mongo/issues)PHPCI failing

Since Jan 20Pushed 6y ago3 watchersCompare

[ Source](https://github.com/dilab/cake-mongo)[ Packagist](https://packagist.org/packages/dilab/cake-mongo)[ RSS](/packages/dilab-cake-mongo/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

CakeMongo - MongoDB Plugin for CakePHP 3
========================================

[](#cakemongo---mongodb-plugin-for-cakephp-3)

[![License](https://camo.githubusercontent.com/e60640ee59d97f7ac9dec83032efb6efa0a698290bedced1e031b086ef7bae51/68747470733a2f2f706f7365722e707567782e6f72672f696d6461642f63616b652d6d6f6e676f2f6c6963656e7365)](https://packagist.org/packages/imdad/cake-mongo) [![Total Downloads](https://camo.githubusercontent.com/454ef62d2b25626abdb2e582969571b6f6d4cc3355163cb71f5fd794ead45001/68747470733a2f2f706f7365722e707567782e6f72672f696d6461642f63616b652d6d6f6e676f2f646f776e6c6f616473)](https://packagist.org/packages/imdad/cake-mongo)

The plugin provides an ORM-like abstraction on top of MongoDB.

Installation
------------

[](#installation)

To install the CakeMongo plugin, you can use composer. From your application’s ROOT directory (where composer.json file is located) run the following:

`composer require imdad/cake-mongo:dev-master`

You will need to add the following line to your application's `config/bootstrap.php` file:

`Plugin::load('Imdad/CakeMongo');`

Additionally, you will need to configure the 'cake\_mongo' datasource connection in your `config/app.php` file. An example configuration would be:

```
'Datasources' => [
    //other datasources
    'cake_mongo' => [
        'className' => 'Imdad\CakeMongo\Datasource\Connection',
        'driver' => 'Imdad\CakeMongo\Datasource\Connection',
    ]
]

```

Overview
--------

[](#overview)

The CakeMongo plugin makes it easier to interact with an MongoDB collections and provides an interface similar to the Database Access &amp; ORM. To get started you should create a **Collection** object. **Collection** objects are the "Repository" or table-like class in CakeMongo:

```
// in src/Model/Collection/ArticlesCollection.php

namespace App\Model\Collection;

use Imdad\CakeMongo\Collection;

class ArticlesCollection extends Collection
{

}
```

Do not confuse CakeMongo's Collection class with CakePHP's Collection class.

You can then use your Collection class in your controllers:

```
public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    // Load the Collection using the 'Mongo' provider.
    $this->loadModel('Articles', 'Mongo');
}

public function add()
{
    $article = $this->Articles->newEntity();
    if ($this->request->is('post')) {
        $article = $this->Articles->patchEntity($article, $this->request->getData());
        if ($this->Articles->save($article)) {
            $this->Flash->success(__('The article has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The article could not be saved. Please, try again.'));
    }
    $this->set(compact('article'));
}
```

We would also need to create a basic view for our indexed articles:

```
