N°7491 - Fix Events during DBObject CRUD

This commit is contained in:
Eric Espie
2024-05-24 09:35:00 +02:00
parent e2f2afad54
commit a0b76a25be
5 changed files with 507 additions and 447 deletions

View File

@@ -5934,7 +5934,7 @@ JS
final protected function FireEventAfterWrite(array $aChanges, bool $bIsNew): void
{
$this->NotifyAttachedObjectsOnLinkClassModification();
$this->FireEventDbLinksChangedForCurrentObject();
$this->RemoveObjectAwaitingEventDbLinksChanged(get_class($this), $this->GetKey());
$this->FireEvent(EVENT_DB_AFTER_WRITE, ['is_new' => $bIsNew, 'changes' => $aChanges]);
}
@@ -6047,31 +6047,6 @@ JS
}
}
/**
* Fire the EVENT_DB_LINKS_CHANGED event if current object is registered
*
* @return void
* @throws \ArchivedObjectException
* @throws \CoreException
*
* @since 3.1.0 N°5906
*/
final protected function FireEventDbLinksChangedForCurrentObject(): void
{
if (true === static::IsEventDBLinksChangedBlocked()) {
return;
}
$sClass = get_class($this);
$sId = $this->GetKey();
$bIsObjectAwaitingEventDbLinksChanged = self::RemoveObjectAwaitingEventDbLinksChanged($sClass, $sId);
if (false === $bIsObjectAwaitingEventDbLinksChanged) {
return;
}
self::FireEventDbLinksChangedForObject($this);
self::RemoveObjectAwaitingEventDbLinksChanged($sClass, $sId);
}
/**
* Fire the EVENT_DB_LINKS_CHANGED event if given object is registered, and unregister it
*
@@ -6110,9 +6085,9 @@ JS
self::SetEventDBLinksChangedBlocked(true);
// N°6408 The object can have been deleted
if (!is_null($oObject)) {
MetaModel::StartReentranceProtection($oObject);
$oObject->FireEvent(EVENT_DB_LINKS_CHANGED);
MetaModel::StopReentranceProtection($oObject);
// Update the object if needed
if (count($oObject->ListChanges()) !== 0) {
$oObject->DBUpdate();
}

View File

@@ -3332,6 +3332,9 @@ abstract class DBObject implements iDisplay
*/
public function DBInsertNoReload()
{
// Prevent DBUpdate at this point (reentrancy protection with temp id)
MetaModel::StartReentranceProtection($this);
$sClass = get_class($this);
$this->AddCurrentObjectInCrudStack('INSERT');
@@ -3378,6 +3381,8 @@ abstract class DBObject implements iDisplay
}
$this->ComputeStopWatchesDeadline(true);
// With temp id
MetaModel::StopReentranceProtection($this);
$iTransactionRetry = 1;
$bIsTransactionEnabled = MetaModel::GetConfig()->Get('db_core_transactions_enabled');
@@ -3570,6 +3575,12 @@ abstract class DBObject implements iDisplay
*/
public function DBUpdate()
{
if (!MetaModel::StartReentranceProtection($this)) {
$this->LogCRUDExit(__METHOD__, 'Rejected (reentrance)');
return false;
}
$this->LogCRUDEnter(__METHOD__);
if (!$this->m_bIsInDB)
{
@@ -3579,12 +3590,6 @@ abstract class DBObject implements iDisplay
$this->AddCurrentObjectInCrudStack('UPDATE');
if (!MetaModel::StartReentranceProtection($this)) {
$this->RemoveCurrentObjectInCrudStack();
$this->LogCRUDExit(__METHOD__, 'Rejected (reentrance)');
return false;
}
try {
// Protect against infinite loop
$this->iUpdateLoopCount++;

View File

@@ -227,6 +227,11 @@
<callback>OnLinksChangedTicket</callback>
<rank>0</rank>
</event_listener>
<event_listener id="UpdateImpactAnalysisLocal">
<event>EVENT_DB_BEFORE_WRITE</event>
<callback>OnBeforeWriteTicket</callback>
<rank>0</rank>
</event_listener>
</event_listeners>
<methods>
<method id="OnLinksChangedTicket">
@@ -237,6 +242,20 @@
public function OnLinksChangedTicket(Combodo\iTop\Service\Events\EventData $oEventData)
{
$this->UpdateImpactedItems();
}
]]></code>
</method>
<method id="OnBeforeWriteTicket">
<static>false</static>
<access>public</access>
<type>EventListener</type>
<code><![CDATA[
public function OnBeforeWriteTicket(Combodo\iTop\Service\Events\EventData $oEventData)
{
$aChanges = $this->ListChanges();
if ($this->IsNew() || array_key_exists('functionalcis_list', $aChanges) || array_key_exists('contacts_list', $aChanges)) {
$this->UpdateImpactedItems();
}
}
]]></code>
</method>

File diff suppressed because it is too large Load Diff

View File

@@ -1,44 +0,0 @@
<?php
/**
* @copyright Copyright (C) 2010-2023 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace DBObject;
use Combodo\iTop\Service\Events\EventData;
use MetaModel;
class MockDBObjectWithCRUDEventListener extends \DBObject
{
const TEST_EVENT = 'test_event';
public $oEventDataReceived = null;
public static function Init()
{
$aParams = array
(
'category' => 'bizmodel, searchable',
'key_type' => 'autoincrement',
'name_attcode' => '',
'state_attcode' => '',
'reconc_keys' => [],
'db_table' => 'priv_unit_tests_mock',
'db_key_field' => 'id',
'db_finalclass_field' => '',
'display_template' => '',
'indexes' => [],
);
MetaModel::Init_Params($aParams);
}
protected function RegisterEventListeners()
{
$this->RegisterCRUDListener(self::TEST_EVENT, 'TestEventCallback', 0, 'unit-test');
}
public function TestEventCallback(EventData $oEventData)
{
$this->oEventDataReceived = $oEventData;
}
}