File tree Expand file tree Collapse file tree 3 files changed +57
-4
lines changed Expand file tree Collapse file tree 3 files changed +57
-4
lines changed Original file line number Diff line number Diff line change @@ -106,9 +106,14 @@ public function getSchema()
106
106
return $ this ->schema ?: $ this ->schema = new Schema ($ this );
107
107
}
108
108
109
- public function remove (Entity $ instance )
109
+ public function remove ($ space , $ params = [] )
110
110
{
111
- $ this ->findRepository ($ instance )->remove ($ instance );
111
+ if ($ space instanceof Entity) {
112
+ $ this ->findRepository ($ space )->removeEntity ($ space );
113
+
114
+ } else {
115
+ $ this ->getRepository ($ space )->remove ($ params );
116
+ }
112
117
}
113
118
114
119
public function save (Entity $ instance )
Original file line number Diff line number Diff line change @@ -179,7 +179,30 @@ public function update(Entity $instance, $operations)
179
179
}
180
180
}
181
181
182
- public function remove ($ instance )
182
+ public function truncate ()
183
+ {
184
+ $ this ->cache = [];
185
+ $ this ->results = [];
186
+ $ id = $ this ->space ->getId ();
187
+ $ this ->space ->getMapper ()->getClient ()->evaluate ("box.space[ $ id]:truncate() " );
188
+ }
189
+
190
+ public function remove ($ params = [])
191
+ {
192
+ if ($ params instanceof Entity) {
193
+ return $ this ->removeEntity ($ params );
194
+ }
195
+
196
+ if (!count ($ params )) {
197
+ throw new Exception ("Use truncate to flush space " );
198
+ }
199
+
200
+ foreach ($ this ->find ($ params ) as $ entity ) {
201
+ $ this ->removeEntity ($ entity );
202
+ }
203
+ }
204
+
205
+ public function removeEntity (Entity $ instance )
183
206
{
184
207
$ key = $ this ->space ->getInstanceKey ($ instance );
185
208
@@ -202,12 +225,12 @@ public function remove($instance)
202
225
$ this ->space ->getMapper ()->getClient ()
203
226
->getSpace ($ this ->space ->getId ())
204
227
->delete ($ pk );
205
-
206
228
}
207
229
208
230
unset($ this ->original [$ key ]);
209
231
210
232
$ this ->results = [];
233
+ $ this ->cache = [];
211
234
}
212
235
213
236
public function save ($ instance )
Original file line number Diff line number Diff line change 6
6
7
7
class MapperTest extends TestCase
8
8
{
9
+ public function testRemove ()
10
+ {
11
+ $ mapper = $ this ->createMapper ();
12
+ $ this ->clean ($ mapper );
13
+
14
+ $ mapper ->getSchema ()->createSpace ('sector_parent ' )
15
+ ->addProperties ([
16
+ 'id ' => 'unsigned ' ,
17
+ 'parent ' => 'unsigned ' ,
18
+ ])
19
+ ->addIndex (['id ' , 'parent ' ]);
20
+
21
+ $ mapper ->create ('sector_parent ' , ['id ' => 1 , 'parent ' => 2 ]);
22
+ $ mapper ->create ('sector_parent ' , ['id ' => 1 , 'parent ' => 3 ]);
23
+ $ mapper ->create ('sector_parent ' , ['id ' => 2 , 'parent ' => 3 ]);
24
+
25
+ $ this ->assertCount (3 , $ mapper ->find ('sector_parent ' ));
26
+
27
+ $ mapper ->remove ('sector_parent ' , ['id ' => 1 ]);
28
+ $ this ->assertCount (1 , $ mapper ->find ('sector_parent ' ));
29
+
30
+ $ mapper ->getRepository ('sector_parent ' )->truncate ();
31
+ $ this ->assertCount (0 , $ mapper ->find ('sector_parent ' ));
32
+ }
33
+
9
34
public function testCompositeKeys ()
10
35
{
11
36
$ mapper = $ this ->createMapper ();
You can’t perform that action at this time.
0 commit comments