Skip to content

Commit 5e96b3c

Browse files
committed
mass remove implemented
1 parent c32558c commit 5e96b3c

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/Mapper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ public function getSchema()
106106
return $this->schema ?: $this->schema = new Schema($this);
107107
}
108108

109-
public function remove(Entity $instance)
109+
public function remove($space, $params = [])
110110
{
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+
}
112117
}
113118

114119
public function save(Entity $instance)

src/Repository.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,30 @@ public function update(Entity $instance, $operations)
179179
}
180180
}
181181

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)
183206
{
184207
$key = $this->space->getInstanceKey($instance);
185208

@@ -202,12 +225,12 @@ public function remove($instance)
202225
$this->space->getMapper()->getClient()
203226
->getSpace($this->space->getId())
204227
->delete($pk);
205-
206228
}
207229

208230
unset($this->original[$key]);
209231

210232
$this->results = [];
233+
$this->cache = [];
211234
}
212235

213236
public function save($instance)

tests/MapperTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66

77
class MapperTest extends TestCase
88
{
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+
934
public function testCompositeKeys()
1035
{
1136
$mapper = $this->createMapper();

0 commit comments

Comments
 (0)