| 14098 |
anikendra |
1 |
# mongoDB datasource for CakePHP
|
|
|
2 |
|
|
|
3 |
[](https://travis-ci.org/ichikaway/cakephp-mongodb)
|
|
|
4 |
[](https://coveralls.io/r/ichikaway/cakephp-mongodb)
|
|
|
5 |
|
|
|
6 |
## Requirements
|
|
|
7 |
|
|
|
8 |
- PHP5
|
|
|
9 |
- CakePHP >= 2.2.5
|
|
|
10 |
- pecl mongo (http://php.net/mongo)
|
|
|
11 |
|
|
|
12 |
## Installation
|
|
|
13 |
|
|
|
14 |
this repository should be installed in the same way as any other plugin.
|
|
|
15 |
|
|
|
16 |
To install the driver for use in a single application:
|
|
|
17 |
|
|
|
18 |
cd my/app/Plugin
|
|
|
19 |
git clone git://github.com/ichikaway/cakephp-mongodb.git Mongodb
|
|
|
20 |
|
|
|
21 |
To install the driver for use in any/multiple application(s)
|
|
|
22 |
|
|
|
23 |
# where ROOT is the name of the directory parent to the base index.php of CakePHP.
|
|
|
24 |
cd ROOT/Plugin
|
|
|
25 |
git clone git://github.com/ichikaway/cakephp-mongodb.git Mongodb
|
|
|
26 |
|
|
|
27 |
### composer
|
|
|
28 |
This plugin on the Packagist.
|
|
|
29 |
|
|
|
30 |
https://packagist.org/packages/ichikaway/cakephp-mongodb
|
|
|
31 |
|
|
|
32 |
## Sample Code
|
|
|
33 |
|
|
|
34 |
To use this DB driver, install (obviously) and define a db source such as follows:
|
|
|
35 |
|
|
|
36 |
<?php
|
|
|
37 |
//app/Config/bootstrap.php
|
|
|
38 |
CakePlugin::load('Mongodb');
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
// app/Config/database.php
|
|
|
42 |
class DATABASE_CONFIG {
|
|
|
43 |
public $default = array(
|
|
|
44 |
'datasource' => 'Mongodb.MongodbSource',
|
|
|
45 |
'host' => 'localhost',
|
|
|
46 |
'database' => 'blog',
|
|
|
47 |
'port' => 27017,
|
|
|
48 |
'prefix' => '',
|
|
|
49 |
'persistent' => 'true',
|
|
|
50 |
/* optional auth fields
|
|
|
51 |
'login' => 'mongo',
|
|
|
52 |
'password' => 'awesomeness',
|
|
|
53 |
'replicaset' => array('host' => 'mongodb://hoge:hogehoge@localhost:27021,localhost:27022/blog',
|
|
|
54 |
'options' => array('replicaSet' => 'myRepl')
|
|
|
55 |
),
|
|
|
56 |
*/
|
|
|
57 |
);
|
|
|
58 |
|
|
|
59 |
// To make sure all tests are passing create the following entry in app/Config/database.php
|
|
|
60 |
public $test = array(
|
|
|
61 |
'datasource' => 'Mongodb.MongodbSource',
|
|
|
62 |
'database' => 'test_mongo',
|
|
|
63 |
'host' => 'localhost',
|
|
|
64 |
'port' => 27017,
|
|
|
65 |
);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
More detail of replicaset in wiki:
|
|
|
69 |
https://github.com/ichikaway/cakephp-mongodb/wiki/How-to-connect-to-replicaset-servers
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
Model files need to have mongoSchema property - or make use of the schemaless behavior.
|
|
|
73 |
|
|
|
74 |
Mongo uses a primary key named "\_id" (cannot be renamed). It can be any format you like but if you don't explicitly set it Mongo will use an automatic 24 character (uu)id.
|
|
|
75 |
|
|
|
76 |
Before you start, you may find it useful to see [a model sample.](http://github.com/ichikaway/mongoDB-Datasource/blob/master/samples/models/post.php)
|
|
|
77 |
There are also some sample [controller actions: find,save,delete,deleteAll,updateAll](http://github.com/ichikaway/mongoDB-Datasource/blob/master/samples/controllers/posts_controller.php) note that your controller code needs no specific code to use this datasource.
|
|
|
78 |
|
|
|
79 |
## Author
|
|
|
80 |
Yasushi Ichikawa ([ichikaway](http://twitter.com/ichikaway))
|
|
|
81 |
|
|
|
82 |
Andy Dawson ([AD7six](http://twitter.com/AD7six))
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
## Contributors
|
|
|
86 |
[Predominant](http://github.com/predominant/) : Cleanup code, add documentation
|
|
|
87 |
|
|
|
88 |
[Jrbasso](http://github.com/jrbasso/) : Cleanup code
|
|
|
89 |
|
|
|
90 |
[tkyk](http://github.com/tkyk/) : Fix bug, Add some function.
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
## Reference
|
|
|
94 |
Reference code, Thank you!
|
|
|
95 |
|
|
|
96 |
[Nate Abele's lithium mongoDB datasource](http://li3.rad-dev.org/)
|
|
|
97 |
|
|
|
98 |
[Joél Perras' divan](http://github.com/jperras/divan/)
|
|
|
99 |
|