/sql/mongodb
[return to app]1
<?php
2 /**
3 * MongoDB helper class
4 */
5 class vorkMongoDB extends MongoDB {
6 /**
7 * Copies a collection
8 *
9 * @param string $from Name of collection
10 * @param string $to Name of the new (copied) collection
11 * @param string $toDb Optional, current DB will be used if omitted
12 * @param array $args Sets the timeout - NOTE: this arg was added in phpMongo driver 1.2.0 for older versions
you
13 * will need to remove the second argument ", $args" from the last line: command($cmd,
$args);
14 * @return array
15 */
16 public function copyCollection($from, $to, $toDb = null, $args = array('timeout' => -1)) {
17 if ($toDb) {
18 $tempName = $from . '_temp_' . time() . rand(1000, 9999);
19 $this->copyCollection($from, $tempName);
20 return $this->renameCollection($tempName, $to, $toDb);
21 }
22 $cmd = 'db.' . addslashes($from) . '.copyTo("' . addslashes($to) . '");';
23 return $this->command(array('$eval' => $cmd), $args);
24 }
25
26 /**
27 * Renames a collection
28 * Can also be used to move a collection to a different database by supplying the third $toDb argument
29 *
30 * @param string $from
31 * @param string $to
32 * @param string $toDb Optional, current DB will be used if omitted
33 * @param array $args Sets the timeout - NOTE: this arg was added in phpMongo driver 1.2.0 for older versions
you
34 * will need to remove the second argument ", $args" from the last line: command($cmd,
$args);
35 * @return array
36 */
37 public function renameCollection($from, $to, $toDb = null, $args = array('timeout' => -1)) {
38 $to = ($toDb ? $toDb : vorkMongo::$dbName) . '.' . $to;
39 $cmd = array('renameCollection' => vorkMongo::$dbName . '.' . $from, 'to' => $to);
40 return vorkMongo::$mongo->selectDB('admin')->command($cmd, $args);
41 }
42 }
43
44 /**
45 * Mongo wrapper - required to extend the MongoDB class
46 */
47 class vorkMongo {
48 /**
49 * Cache of Mongo object
50 * @var Mongo
51 */
52 static $mongo;
53
54 /**
55 * Name of MongoDB
56 * @var string
57 */
58 static $dbName;
59
60 /**
61 * Wrapper for the Mongo object
62 *
63 * @param string $server
64 * @param array $options
65 * @return Mongo
66 */
67 public function __construct($server = 'mongodb://localhost:27017', array $options = array('connect' => true))
{
68 self::$mongo = new Mongo($server, $options);
69 return self::$mongo;
70 }
71
72 /**
73 * Wrapper for the Mongo::selectDB() method
74 *
75 * @param string $dbName
76 * @return MongoDB
77 */
78 public function selectDB($dbName) {
79 self::$dbName = $dbName;
80 return new vorkMongoDB(self::$mongo, $dbName);
81 }
82
83 /**
84 * Wrapper for shortcut to selectDB()
85 *
86 * @param string $dbName
87 * @return MongoDB
88 */
89 public function __get($dbName) {
90 return $this->selectDB($dbName);
91 }
92
93 /**
94 * Returns a string-representation of the Mongo object
95 * @return string
96 */
97 public function __toString() {
98 return (string) self::$mongo;
99 }
100
101 /**
102 * Catch-all method to future-proof this class
103 *
104 * @param string $name
105 * @param array $args
106 * @return mixed
107 */
108 public function __call($name, array $args) {
109 return call_user_func_array(array(self::$mongo, $name), $args);
110 }
111 }