3
3
namespace Doctrine \ODM \MongoDB \Persisters ;
4
4
5
5
use Doctrine \ODM \MongoDB \DocumentManager ,
6
- Doctrine \ODM \MongoDB \Mapping \ClassMetadata ,
7
- Doctrine \ODM \MongoDB \MongoCursor ,
8
- Doctrine \ODM \MongoDB \Mapping \Types \Type ,
9
- Doctrine \Common \Collections \Collection ;
6
+ Doctrine \ODM \MongoDB \Mapping \ClassMetadata ,
7
+ Doctrine \ODM \MongoDB \MongoCursor ,
8
+ Doctrine \ODM \MongoDB \Mapping \Types \Type ,
9
+ Doctrine \Common \Collections \Collection ;
10
10
11
11
/**
12
12
* @author Bulat Shakirzyanov <bulat@theopenskyproject.com>
13
13
*/
14
14
class BasicDocumentPersister
15
15
{
16
- protected $ _dm ;
17
- protected $ _uow ;
18
- protected $ _class ;
19
- protected $ _collection ;
20
- protected $ _documentName ;
21
- protected $ _documentIdentifiers = array ();
22
- protected $ _queuedInserts = array ();
23
- public function __construct (DocumentManager $ dm , ClassMetadata $ class )
24
- {
25
- $ this ->_dm = $ dm ;
26
- $ this ->_uow = $ dm ->getUnitOfWork ();
27
- $ this ->_class = $ class ;
28
- $ this ->_documentName = $ class ->getName ();
16
+ protected $ _dm ;
17
+ protected $ _uow ;
18
+ protected $ _class ;
19
+ protected $ _collection ;
20
+ protected $ _documentName ;
21
+ protected $ _documentIdentifiers = array ();
22
+ protected $ _queuedInserts = array ();
23
+ public function __construct (DocumentManager $ dm , ClassMetadata $ class )
24
+ {
25
+ $ this ->_dm = $ dm ;
26
+ $ this ->_uow = $ dm ->getUnitOfWork ();
27
+ $ this ->_class = $ class ;
28
+ $ this ->_documentName = $ class ->getName ();
29
29
$ this ->_collection = $ dm ->getDocumentCollection ($ class ->name );
30
- }
30
+ }
31
31
public function addInsert ($ document )
32
- {
32
+ {
33
33
$ this ->_queuedInserts [spl_object_hash ($ document )] = $ document ;
34
- }
34
+ }
35
35
public function executeInserts ()
36
- {
36
+ {
37
37
if ( ! $ this ->_queuedInserts ) {
38
38
return ;
39
39
}
40
40
41
41
$ postInsertIds = array ();
42
- $ inserts = array ();
42
+ $ inserts = array ();
43
43
44
44
foreach ($ this ->_queuedInserts as $ oid => $ document ) {
45
- $ data = $ this ->_prepareInsertData ($ document );
45
+ $ data = $ this ->_prepareInsertData ($ document );
46
46
$ inserts [$ oid ] = $ data ;
47
- }
48
- $ this ->_collection ->batchInsert ($ inserts );
49
-
50
- foreach ($ inserts as $ oid => $ data ) {
51
- $ document = $ this ->_queuedInserts [$ oid ];
52
- $ postInsertIds [(string ) $ data ['_id ' ]] = $ document ;
53
- if ($ this ->_class ->isFile ()) {
54
- $ this ->_dm ->getHydrator ()->hydrate ($ this ->_class , $ document , $ data );
55
- }
56
- }
57
-
58
- return $ postInsertIds ;
59
- }
47
+ }
48
+ $ this ->_collection ->batchInsert ($ inserts );
49
+
50
+ foreach ($ inserts as $ oid => $ data ) {
51
+ $ document = $ this ->_queuedInserts [$ oid ];
52
+ $ postInsertIds [(string ) $ data ['_id ' ]] = $ document ;
53
+ if ($ this ->_class ->isFile ()) {
54
+ $ this ->_dm ->getHydrator ()->hydrate ($ this ->_class , $ document , $ data );
55
+ }
56
+ }
57
+
58
+ return $ postInsertIds ;
59
+ }
60
60
public function update ($ document )
61
- {
62
- $ update = $ this ->_prepareUpdateData ($ document );
63
- $ id = $ update ['_id ' ];
64
- unset($ update ['_id ' ]);
61
+ {
62
+ $ update = $ this ->_prepareUpdateData ($ document );
63
+ $ id = $ update ['_id ' ];
64
+ unset($ update ['_id ' ]);
65
65
66
- $ this ->_collection ->update (array ('_id ' => $ id ), array ('$set ' => $ update ));
67
- }
66
+ $ this ->_collection ->update (array ('_id ' => $ id ), array ('$set ' => $ update ));
67
+ }
68
68
public function delete ($ document )
69
- {
69
+ {
70
70
$ id = $ this ->_uow ->getDocumentIdentifier ($ document );
71
- $ this ->_collection ->remove (array ('_id ' => new \MongoId ($ id )));
72
- }
73
-
74
- private function _prepareInsertData ($ document )
75
- {
76
- return $ this ->_prepareUpdateData ($ document );
77
- }
78
- private function _prepareUpdateData ($ document )
79
- {
71
+ $ this ->_collection ->remove (array ('_id ' => new \MongoId ($ id )));
72
+ }
73
+
74
+ private function _prepareInsertData ($ document )
75
+ {
76
+ return $ this ->_prepareUpdateData ($ document );
77
+ }
78
+ private function _prepareUpdateData ($ document )
79
+ {
80
80
$ oid = spl_object_hash ($ document );
81
81
$ changeset = $ this ->_uow ->getDocumentChangeSet ($ document );
82
82
foreach ($ changeset as $ fieldName => $ values ) {
83
83
$ changeset [$ fieldName ] = $ values [1 ];
84
84
}
85
- $ docId = $ this ->_uow ->getDocumentIdentifier ($ document );
85
+ $ docId = $ this ->_uow ->getDocumentIdentifier ($ document );
86
86
if ($ docId ) {
87
87
$ changeset ['_id ' ] = new \MongoId ($ docId );
88
88
}
@@ -116,7 +116,7 @@ private function _prepareUpdateData($document)
116
116
}
117
117
}
118
118
return $ changeset ;
119
- }
119
+ }
120
120
121
121
/**
122
122
* Gets the ClassMetadata instance of the entity class this persister is used for.
@@ -128,9 +128,9 @@ public function getClassMetadata()
128
128
return $ this ->_class ;
129
129
}
130
130
public function refresh (array $ id , $ document )
131
- {
132
-
133
- }
131
+ {
132
+
133
+ }
134
134
135
135
/**
136
136
* Loads an entity by a list of field criteria.
@@ -153,13 +153,13 @@ public function load(array $query = array(), array $select = array())
153
153
}
154
154
155
155
public function loadById ($ id )
156
- {
157
- $ result = $ this ->_collection ->findOne (array ('_id ' => new \MongoId ($ id )));
158
- if ($ result !== null ) {
159
- return $ this ->_uow ->getOrCreateDocument ($ this ->_documentName , $ result );
160
- }
161
- return null ;
162
- }
156
+ {
157
+ $ result = $ this ->_collection ->findOne (array ('_id ' => new \MongoId ($ id )));
158
+ if ($ result !== null ) {
159
+ return $ this ->_uow ->getOrCreateDocument ($ this ->_documentName , $ result );
160
+ }
161
+ return null ;
162
+ }
163
163
164
164
/**
165
165
* Loads a list of entities by a list of field criteria.
@@ -168,10 +168,10 @@ public function loadById($id)
168
168
* @return array
169
169
*/
170
170
public function loadAll (array $ query = array (), array $ select = array ())
171
- {
172
- $ cursor = $ this ->_collection ->find ($ query , $ select );
173
- return new MongoCursor ($ this ->_dm , $ this ->_dm ->getHydrator (), $ this ->_class , $ cursor );
174
- }
171
+ {
172
+ $ cursor = $ this ->_collection ->find ($ query , $ select );
173
+ return new MongoCursor ($ this ->_dm , $ this ->_dm ->getHydrator (), $ this ->_class , $ cursor );
174
+ }
175
175
176
176
/**
177
177
* returns the reference representation to be stored in mongodb
@@ -183,12 +183,12 @@ public function loadAll(array $query = array(), array $select = array())
183
183
private function _prepareDocReference ($ class , $ doc )
184
184
{
185
185
$ id = $ this ->_uow ->getDocumentIdentifier ($ doc );
186
- $ ref = array (
187
- '$ref ' => $ class ->getCollection (),
188
- '$id ' => $ id ,
189
- '$db ' => $ class ->getDB ()
190
- );
191
- return $ ref ;
186
+ $ ref = array (
187
+ '$ref ' => $ class ->getCollection (),
188
+ '$id ' => $ id ,
189
+ '$db ' => $ class ->getDB ()
190
+ );
191
+ return $ ref ;
192
192
}
193
193
194
194
/**
0 commit comments