Skip to content

Commit 0a668d3

Browse files
onbjergtimgws
authored andcommitted
Fix inherited collection methods
* Add setInstance method (Fixes #48, #47, #12 and #6) * Remove redundant chunk method
1 parent e72bcc4 commit 0a668d3

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/ElasticquentResultCollection.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ElasticquentResultCollection extends \Illuminate\Database\Eloquent\Collect
1717
* @param $results elasticsearch results
1818
* @param $instance
1919
*/
20-
public function __construct($results, $instance)
20+
public function __construct($results, $instance = null)
2121
{
2222
// Take our result data and map it
2323
// to some class properties.
@@ -28,13 +28,29 @@ public function __construct($results, $instance)
2828
$this->aggregations = isset($results['aggregations']) ? $results['aggregations'] : array();
2929

3030
// Save the instance we performed the search on.
31-
$this->instance = $instance;
31+
// This is only done when Elasticquent creates the collection at first.
32+
if ($instance !== null) {
33+
$this->instance = $instance;
34+
}
3235

3336
// Now we need to assign our hits to the
3437
// items in the collection.
3538
$this->items = $this->hitsToItems($instance);
3639
}
3740

41+
/**
42+
* Set the model instance we performed the search on.
43+
*
44+
* @param $instance
45+
* @return $this
46+
*/
47+
public function setInstance($instance)
48+
{
49+
$this->instance = $instance;
50+
51+
return $this;
52+
}
53+
3854
/**
3955
* Hits To Items
4056
*
@@ -143,22 +159,4 @@ public function paginate($pageLimit = 25)
143159

144160
return new Paginator($sliced_items, $this->hits, $this->totalHits(), $pageLimit, $page, ['path' => Paginator::resolveCurrentPath()]);
145161
}
146-
147-
/**
148-
* Chunk the underlying collection array.
149-
*
150-
* @param int $size
151-
* @param bool $preserveKeys
152-
* @return static
153-
*/
154-
public function chunk($size, $preserveKeys = false)
155-
{
156-
$chunks = [];
157-
158-
foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
159-
$chunks[] = new static($chunk, $this->instance);
160-
}
161-
162-
return new static($chunks, $this->instance);
163-
}
164162
}

0 commit comments

Comments
 (0)