Skip to content

Commit 6d18ba9

Browse files
Firetawnyowlnekufa
authored andcommitted
index casting fix
1 parent 4af2483 commit 6d18ba9

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/Space.php

+15
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ public function castIndex(array $fields): ?array
9393
}
9494
}
9595

96+
foreach ($this->indexes as $index) {
97+
$check = false;
98+
foreach ($fields as $field) {
99+
if (in_array($field, array_slice($index['fields'], 0, count($fields)))) {
100+
$check = true;
101+
} else {
102+
$check = false;
103+
break;
104+
}
105+
}
106+
if ($check) {
107+
return $index;
108+
}
109+
}
110+
96111
throw new Exception("Index casting failure");
97112
}
98113

tests/MapperTest.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,38 @@ public function testCreateRow()
152152
$tester->drop();
153153
}
154154

155+
public function testIndexCasting()
156+
{
157+
$mapper = $this->createMapper();
158+
159+
$mapper->arrays = true;
160+
161+
foreach ($mapper->find('_vspace') as $space) {
162+
if ($space['id'] >= 512) {
163+
$mapper->getSpace($space['id'])->drop();
164+
}
165+
}
166+
167+
$tester = $mapper->createSpace('tester');
168+
169+
$tester->addProperty('id', 'unsigned');
170+
$tester->addProperty('name', 'string');
171+
$tester->addProperty('nick', 'string');
172+
$tester->addProperty('age', 'unsigned');
173+
$tester->addIndex(['name', 'nick', 'age']);
174+
175+
$testRow = ['id' => 1, 'name' => 'Vladimir', 'nick' => 'vovan1', 'age' => 20];
176+
$tester->create($testRow);
177+
178+
$row1 = $mapper->find('tester', ['name' => 'Vladimir', 'nick' => 'vovan1']);
179+
$row2 = $mapper->find('tester', ['nick' => 'vovan1', 'name' => 'Vladimir']);
180+
$row3 = $mapper->find('tester', ['name' => 'Vladimir', 'age' => 20, 'nick' => 'vovan1']);
181+
182+
$this->assertSame($row1, $row2);
183+
$this->assertSame($row1, $row3);
184+
$tester->drop();
185+
}
186+
155187
public function testFindOrCreateRow()
156188
{
157189
$mapper = $this->createMapper();
@@ -201,7 +233,7 @@ public function testFindOrCreateRow()
201233
$this->assertTrue($result[0][0] == 2);
202234
$this->assertSame($secondRow->id, $result[0][0]);
203235
$this->assertEquals($secondRow, $findRow);
204-
$tester->drop();
236+
205237
}
206238

207239
public function testLua()

0 commit comments

Comments
 (0)