Skip to content

Commit d2a6155

Browse files
committed
Some improvements and simplifications for get() function, fixes for correct reloading of nonexistent models in result collection.
1 parent cfb68bf commit d2a6155

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

src/Nqxcode/LuceneSearch/Query/Builder.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,15 @@ public function limit($limit, $offset = 0)
6060
*/
6161
public function get()
6262
{
63-
$models = $this->runner->getCachedModels($this->query, $this->limit, $this->offset);
63+
$models = $this->runner->getCachedModels($this->query);
6464
if (null === $models) {
6565
$models = $this->runner->models($this->query);
66-
67-
$this->runner->setCachedModels($this->query, $models, $this->limit, $this->offset);
66+
$this->runner->setCachedModels($this->query, $models);
6867
$this->runner->setCachedTotal($this->query, $models->count());
6968
}
70-
7169
if ($this->limit) {
7270
$models = $models->slice($this->offset, $this->limit);
7371
}
74-
7572
return Collection::make($models->reload()->all());
7673
}
7774

src/Nqxcode/LuceneSearch/Query/Runner.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,23 @@ public function delete($model)
9696
* Get cached models for query.
9797
*
9898
* @param $query
99-
* @param $limit
100-
* @param $offset
10199
* @return null|int
102100
*/
103-
public function getCachedModels($query, $limit = null, $offset = null)
101+
public function getCachedModels($query)
104102
{
105-
$hash = $this->hash($query, compact('limit', 'offset'));
103+
$hash = $this->hash($query);
106104
return isset($this->cachedModels[$hash]) ? $this->cachedModels[$hash] : null;
107105
}
108106

109107
/**
110108
* Set cached models for query.
111109
*
112110
* @param $query
113-
* @param $limit
114-
* @param $offset
115111
* @param $models
116112
*/
117-
public function setCachedModels($query, $models, $limit = null, $offset = null)
113+
public function setCachedModels($query, $models)
118114
{
119-
$hash = $this->hash($query, compact('limit', 'offset'));
115+
$hash = $this->hash($query);
120116
$this->cachedModels[$hash] = $models;
121117
}
122118

@@ -148,12 +144,11 @@ public function setCachedTotal($query, $total)
148144
* Get hash for query.
149145
*
150146
* @param $query
151-
* @param $options
152147
* @return string
153148
*/
154-
private function hash($query, array $options = [])
149+
private function hash($query)
155150
{
156-
return md5(serialize($query) . serialize($options));
151+
return md5(serialize($query));
157152
}
158153

159154
/**

src/Nqxcode/LuceneSearch/Support/Collection.php

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,26 @@
44

55
class Collection extends \Illuminate\Database\Eloquent\Collection
66
{
7-
protected $reloaded = false;
8-
97
/**
10-
* Reload each item in collection.
8+
* Reload each nonexistent item in collection.
119
*
1210
* @return $this|static
1311
*/
1412
public function reload()
1513
{
16-
if (!$this->reloaded) {
17-
$this->reloaded = true;
18-
19-
$items = array_map(
20-
function (Model $sourceModel) {
21-
$primaryKey = $sourceModel->getKeyName();
22-
$primaryValue = $sourceModel->{$primaryKey};
23-
24-
$query = $sourceModel->query();
25-
$query->where($primaryKey, $primaryValue);
26-
27-
$targetModel = $query->first();
28-
29-
return $targetModel;
30-
},
31-
$this->items
32-
);
33-
34-
$items = array_filter(
35-
$items,
36-
function ($item) {
37-
return !is_null($item);
14+
/** @var Model $source */
15+
foreach ($this->items as $i => $source) {
16+
if (!$source->exists) {
17+
/** @var Model $target */
18+
$target = $source->find($source->{$source->getKeyName()});
19+
if (!is_null($target)) {
20+
$source->setRawAttributes($target->getAttributes(), true);
21+
$source->exists = true;
22+
} else {
23+
unset($this->items[$i]);
3824
}
39-
);
40-
41-
$this->items = $items;
25+
}
4226
}
43-
4427
return $this;
4528
}
4629
}

tests/functional/SearchTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,29 @@ public function testSearchWithPaginate()
9595
public function testGetWithLimit()
9696
{
9797
$query = Search::query('clock,pointer', '*', ['phrase' => false]);
98+
99+
$query->limit(3, 3);
100+
$found = $query->get();
101+
102+
$this->assertCount(3, $found);
103+
$this->assertCount(3, $found->filter(function ($v) {
104+
return $v->exists;
105+
}));
106+
107+
$query->limit(null);
98108
$found = $query->get();
99109

100110
$this->assertCount(6, $found);
101-
$this->assertCount(6, $found->filter(function($v){ return $v->exists;}));
111+
$this->assertCount(6, $found->filter(function ($v) {
112+
return $v->exists;
113+
}));
102114

103-
$query->limit(3, 3);
115+
$query->limit(3, 0);
104116
$found = $query->get();
105117

106118
$this->assertCount(3, $found);
107-
$this->assertCount(3, $found->filter(function($v){ return $v->exists;}));
119+
$this->assertCount(3, $found->filter(function ($v) {
120+
return $v->exists;
121+
}));
108122
}
109123
}

0 commit comments

Comments
 (0)