Skip to content

Commit 36049fe

Browse files
committed
reject values logic added
1 parent 8773a6c commit 36049fe

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/Repository.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,20 @@ public function find($params = [], $oneItem = false)
122122
$params = [$params];
123123
}
124124

125+
$reject = [];
125126
if (is_array($params)) {
127+
128+
foreach($params as $key => $value) {
129+
if(is_string($value)) {
130+
if(substr($value, 0, 1) == '!') {
131+
$reject[$key] = substr($value, 1);
132+
}
133+
}
134+
}
135+
foreach(array_keys($reject) as $key) {
136+
unset($params[$key]);
137+
}
138+
126139
foreach ($params as $key => $value) {
127140
if (is_numeric($key) && $value instanceof Contracts\Entity) {
128141
$type = $this->type->getManager()->findRepository($value)->getType();
@@ -137,7 +150,7 @@ public function find($params = [], $oneItem = false)
137150
}
138151
}
139152

140-
$findKey = md5(json_encode($query).($oneItem ? 'x' : ''));
153+
$findKey = md5(json_encode([$query, $reject]).($oneItem ? 'x' : ''));
141154
if (array_key_exists($findKey, $this->findCache)) {
142155
return $this->findCache[$findKey];
143156
}
@@ -148,6 +161,26 @@ public function find($params = [], $oneItem = false)
148161
}
149162

150163
$values = count($query) ? $this->type->getIndexTuple($index, $query) : [];
164+
165+
if (count($reject)) {
166+
$if = [];
167+
$properties = $this->type->getProperties();
168+
foreach ($reject as $key => $value) {
169+
$num = array_search($key, $properties) + 1;
170+
if(!$num) {
171+
throw new Exception("Unknown property $key");
172+
}
173+
$if[] = "tuple[".$num.'] ~= '.(is_numeric($value) ? intval($value):"'$value'");
174+
}
175+
176+
return $this->evaluate("
177+
local result = {}
178+
for _, tuple in box.space.".$this->type->getName().'.index['.$index.']:pairs{'.implode(',', $values)."} do
179+
if (" . implode(" && ", $if).") then table.insert(result, tuple) end
180+
end
181+
return result");
182+
}
183+
151184
$data = $this->type->getSpace()->select($values, $index);
152185

153186
$result = [];

tests/MapperTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,37 @@
44

55
class MapperTest extends PHPUnit_Framework_TestCase
66
{
7+
public function testNotEquals()
8+
{
9+
$manager = Helper::createManager();
10+
$person = $manager->getMeta()->create('person', ['name']);
11+
$manager->create('person', 'dmitry');
12+
$manager->create('person', 'vasiliy');
13+
$manager->create('person', 'vladimir');
14+
15+
$this->assertCount(2, $manager->get('person')->find(['id' => '!2']));
16+
17+
$task = $manager->getMeta()->create('task', ['unit', 'sector', 'title']);
18+
$task->setPropertyType(['unit', 'sector'], 'integer');
19+
$task->addIndex(['unit'], ['unique' => false]);
20+
21+
$manager->create('task', ['unit' => 1, 'sector' => 1]);
22+
$manager->create('task', ['unit' => 2, 'sector' => 2]);
23+
$manager->create('task', ['unit' => 1, 'sector' => 2]);
24+
25+
$this->assertCount(2, $manager->get('task')->find([
26+
'unit' => 1
27+
]));
28+
29+
$this->assertCount(1, $manager->get('task')->find([
30+
'unit' => 1, 'sector' => '!2'
31+
]));
32+
33+
$this->assertCount(1, $manager->get('task')->find([
34+
'unit' => "!1"
35+
]));
36+
}
37+
738
public function testEvalution()
839
{
940
$manager = Helper::createManager();

0 commit comments

Comments
 (0)