Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit f29e531

Browse files
committed
- An index isn't updated if model data is empty
- Fixed the problem when model observer was registered twice
1 parent 3319615 commit f29e531

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

src/Console/ElasticUpdateMappingCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class ElasticUpdateMappingCommand extends Command
1616
protected $description = 'Update a model mapping';
1717

1818
public function handle() {
19-
$model = $this->getModel();
19+
if (!$model = $this->getModel()) {
20+
return;
21+
}
2022

2123
$configurator = $model->getIndexConfigurator();
2224

src/Console/Features/requiresModelArgument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function getModel()
1616

1717
$modelInstance = new $modelClass;
1818

19-
if (!($modelInstance instanceof Model) || !method_exists($modelInstance, 'getIndexConfigurator')) {
19+
if (!($modelInstance instanceof Model) || !in_array(Searchable::class, class_uses_recursive($modelClass))) {
2020
$this->error(sprintf(
2121
'The %s class must extend %s and use the %s trait.',
2222
$modelClass,

src/ElasticEngine.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ public function __construct()
2424

2525
public function update($models)
2626
{
27-
$models->map(function ($model) {
27+
$models->each(function ($model) {
2828
if ($this->updateMapping) {
2929
Artisan::call(
3030
'elastic:update-mapping',
3131
['model' => get_class($model)]
3232
);
3333
}
3434

35+
$array = $model->toSearchableArray();
36+
37+
if (empty($array)) {
38+
return true;
39+
}
40+
3541
$payload = (new DocumentPayload($model))
36-
->setIfNotEmpty('body', $model->toSearchableArray())
42+
->set('body', $array)
3743
->get();
3844

3945
ElasticClient::index($payload);
@@ -44,7 +50,7 @@ public function update($models)
4450

4551
public function delete($models)
4652
{
47-
$models->map(function ($model) {
53+
$models->each(function ($model) {
4854
$payload = (new DocumentPayload($model))
4955
->get();
5056

src/Payloads/TypePayload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TypePayload extends IndexPayload
1515

1616
public function __construct(Model $model)
1717
{
18-
if (!method_exists($model, 'getIndexConfigurator')) {
18+
if (!in_array(Searchable::class, class_uses_recursive($model))) {
1919
throw new Exception(sprintf(
2020
'The %s model must use the %s trait.',
2121
get_class($model),

src/Searchable.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@
88
use \Exception;
99

1010
trait Searchable {
11-
use ScoutSearchable;
11+
use ScoutSearchable {
12+
ScoutSearchable::bootSearchable as bootScoutSearchable;
13+
}
14+
15+
private static $isSearchableTraitBooted = false;
16+
17+
public static function bootSearchable()
18+
{
19+
if (self::$isSearchableTraitBooted) {
20+
return;
21+
}
22+
23+
self::bootScoutSearchable();
24+
25+
self::$isSearchableTraitBooted = true;
26+
}
1227

1328
/**
1429
* @return IndexConfigurator

0 commit comments

Comments
 (0)