diff --git a/lib/model/QubitActor.php b/lib/model/QubitActor.php index cf4141d0bd..f19c403317 100644 --- a/lib/model/QubitActor.php +++ b/lib/model/QubitActor.php @@ -136,30 +136,30 @@ public function save($connection = null) parent::save($connection); - $creationIoIds = $otherIoIds = []; $context = sfContext::getInstance(); $env = $context->getConfiguration()->getEnvironment(); // Save related event objects - foreach ($this->events as $event) { - $event->indexOnSave = false; - - // Update search index for related info object, update them - // in QubitEvent synchronously in CLI tasks and jobs - if (in_array($env, ['cli', 'worker'])) { + if (in_array($env, ['cli', 'worker'])) { + // In CLI/worker environments, all events are saved with indexing enabled + // so related information objects are updated synchronously in QubitEvent. + foreach ($this->events as $event) { $event->indexOnSave = true; - } elseif (isset($event->objectId)) { - // Otherwise, do not update in QubitEvent, - // but save ids to update asynchronously - if (isset($event->typeId) && QubitTerm::CREATION_ID == $event->typeId) { - $creationIoIds[] = $event->objectId; - } else { - $otherIoIds[] = $event->objectId; + $event->actor = $this; + $event->save(); + } + } else { + // Only iterate over newly created but not saved events (i.e., transient events). This + // avoids looping over all existing events, which are already saved in the event edit + // component + foreach ($this->events->transient as $event) { + if (isset($event->new) && $event->new) { + // We don't index on save because we index below in arUpdateEsIoDocumentsJob + $event->indexOnSave = false; + $event->actor = $this; + $event->save(); } } - - $event->actor = $this; - $event->save(); } // Save related contact information objects @@ -169,6 +169,20 @@ public function save($connection = null) } if ($this->indexOnSave) { + // Find creation-type events + $sql = 'SELECT DISTINCT object_id FROM ' + .QubitEvent::TABLE_NAME + .' WHERE actor_id = ? AND type_id = ? AND object_id IS NOT NULL'; + + $creationIoIds = QubitPdo::fetchAll($sql, [$this->id, QubitTerm::CREATION_ID], ['fetchMode' => PDO::FETCH_COLUMN]); + + // Find other non-creation-type events + $sql = 'SELECT DISTINCT object_id FROM ' + .QubitEvent::TABLE_NAME + .' WHERE actor_id = ? AND (type_id != ? OR type_id IS NULL) AND object_id IS NOT NULL'; + + $otherIoIds = QubitPdo::fetchAll($sql, [$this->id, QubitTerm::CREATION_ID], ['fetchMode' => PDO::FETCH_COLUMN]); + // Update asynchronously the saved IOs ids, two jobs may // be launched in here as creation events require updating // the descendants but other events don't.