-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoPOO.php
133 lines (107 loc) · 3.6 KB
/
MongoPOO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
class MongoPOO {
private $manager;
private $query;
private $bulk;
public static $dsn = "mongodb://localhost:27017";
public static $dbName = "MediaMongo";
public static $collectionBooks = "livres";
public static $collectionUsers = "users";
/**
* Initialise le manager
*/
function __construct($dsn) {
$this->manager = new MongoDB\Driver\Manager($dsn);
}
/**
* Récupère le manager
* @return $manager
*/
private function getManager() {
return $this->manager;
}
/**
* Get the query
* @return $query
*/
private function getQuery() {
return $this->query;
}
/**
* Get the id of the object
*/
public function getMongoDbObjectId($instance, $dbName, $collection, $filter = null) {
$this->query = $instance->initQuery($dbName, $collection, $filter);
foreach($this->query as $row) {
$id = $row->_id;
}
return $id;
}
/**
* Requête avec ou sans filtre
* @return $query
*/
public function initQuery($dbName, $collection, $filter = null) {
if($filter === null) {
$this->query = new MongoDB\Driver\Query([]);
} else {
$this->query = new MongoDB\Driver\Query($filter);
}
$this->query = $this->getManager()->executeQuery($dbName . "." . $collection, $this->getQuery());
return $this->getQuery();
}
public function initCommand($collection, $filter = null) {
$command = $this->getManager()->executeCommand($collection, $filter);
return $command;
}
/**
* Ajout des données à une collection
*/
public function addDatas($dbName, $collection, $datasToAdd) {
$this->bulk = new MongoDB\Driver\BulkWrite;
foreach($datasToAdd as $row) {
$this->bulk->insert($row);
}
$this->manager->executeBulkWrite($dbName . "." . $collection, $this->bulk);
}
/**
* Met à jour des données d'une collection
*/
public function updateDatas($dbName, $collection, $filter, $datasToUpdate) {
$this->bulk = new MongoDB\Driver\BulkWrite;
$this->bulk->update($filter, $datasToUpdate);
$this->manager->executeBulkWrite($dbName . "." . $collection, $this->bulk);
}
/**
* Supprime des données d'une collection
*/
public function deleteDatas($dbName, $collection, $datasToDelete) {
$this->bulk = new MongoDB\Driver\BulkWrite;
$this->manager = $this->getManager();
// $this->bulk->delete(['name' => "Poke"], ['limit' => 1]);
foreach($datasToDelete as $data) {
$this->bulk->delete($data);
}
$this->manager->executeBulkWrite($dbName . "." . $collection, $this->bulk);
}
/**
* Efface une collection
*/
public function deleteCollection($dbName, $collection) {
$this->bulk = new MongoDB\Driver\BulkWrite;
$this->manager = $this->getManager();
$this->bulk->delete([]);
$this->manager->executeBulkWrite($dbName . "." . $collection, $this->bulk);
}
// public function displaySpells($dbName, $collectionSpells, $filter = null) {
// $query = $this->initQuery($dbName, $collectionSpells, $filter);
// echo'Level 1 spell list:<br/>';
// foreach ($query as $row) {
// echo 'Spell name: ' . $row->name . "<br>";
// echo 'Spell level: ' . $row->level . "<br>";
// if(isset($row->flavor)) {
// echo " (Flavor: " . $row->flavor . ")<br>";
// }
// }
// }
}