From 172eab9a6e3bd35d8b0cc21c7454f71d1e80e842 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Wed, 30 Dec 2020 22:56:57 -0500 Subject: [PATCH] Declare afterCommit in the ModelObserver and add config option and constructor --- config/scout.php | 13 +++++++++++++ src/ModelObserver.php | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/config/scout.php b/config/scout.php index d8f0f3bb..8b3e3c62 100644 --- a/config/scout.php +++ b/config/scout.php @@ -43,6 +43,19 @@ 'queue' => env('SCOUT_QUEUE', false), + /* + |-------------------------------------------------------------------------- + | Database Transactions + |-------------------------------------------------------------------------- + | + | This option allows you to control if the operations that sync your + | data with your search engines are only executed afterall open + | database transactions have committed. + | + */ + + 'after_commit' => false, + /* |-------------------------------------------------------------------------- | Chunk Sizes diff --git a/src/ModelObserver.php b/src/ModelObserver.php index 4d0da75c..8e4fe672 100644 --- a/src/ModelObserver.php +++ b/src/ModelObserver.php @@ -6,6 +6,13 @@ class ModelObserver { + /** + * Only dispatch the observer's events after all database transactions have committed. + * + * @var bool + */ + public $afterCommit; + /** * The class names that syncing is disabled for. * @@ -13,6 +20,16 @@ class ModelObserver */ protected static $syncingDisabledFor = []; + /** + * Create a new observer instance. + * + * @return void + */ + public function __construct() + { + $this->afterCommit = config('scout.after_commit', false); + } + /** * Enable syncing for the given class. *