2017 © Pedro PelĂĄez
 

cakephp-plugin cake-mongo

A MongoDB datasource and data mapper for CakePHP 3.0

image

dilab/cake-mongo

A MongoDB datasource and data mapper for CakePHP 3.0

  • Friday, December 8, 2017
  • by xuding
  • Repository
  • 3 Watchers
  • 5 Stars
  • 140 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 5 Open issues
  • 1 Versions
  • 32 % Grown

The README.md

CakeMongo - MongoDB Plugin for CakePHP 3

License Total Downloads, (*1)

The plugin provides an ORM-like abstraction on top of MongoDB., (*2)

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:, (*3)

composer require imdad/cake-mongo:dev-master, (*4)

You will need to add the following line to your application's config/bootstrap.php file:, (*5)

Plugin::load('Imdad/CakeMongo');, (*6)

Additionally, you will need to configure the 'cake_mongo' datasource connection in your config/app.php file. An example configuration would be:, (*7)

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

Overview

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

// 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., (*9)

You can then use your Collection class in your controllers:, (*10)


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:, (*11)

<?= $this->Form->create($article); ?>
<?= $this->Form->control('title', ['empty' => true]); ?>
<?= $this->Form->control('body', ['type' => 'textarea']); ?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

You should now be able to submit the form and have a new document added to MongoDB., (*12)

Alternatively you can load Collection anywhere you want using CollectionRegistry:, (*13)

use Imdad\CakeMongo\CollectionRegistry;

$this->Articles = CollectionRegistry::get('Articles');

Support

GitHub Issues - Submit bug/issue here! Please supply as much information as possible when submitting a bug. It will the best if you could create a Unit Test., (*14)

The Versions

08/12 2017

dev-master

9999999-dev

A MongoDB datasource and data mapper for CakePHP 3.0

  Sources   Download

MIT

The Requires

 

The Development Requires