Add EVENT_DB_BEFORE_CREATE and EVENT_DB_BEFORE_UPDATE events in CRUD calls

This commit is contained in:
Eric Espie
2023-05-16 15:52:02 +02:00
parent 59cd489e72
commit ad25d8823f
4 changed files with 74 additions and 1 deletions

View File

@@ -5877,6 +5877,11 @@ JS
$this->FireEvent(EVENT_DB_CHECK_TO_WRITE);
}
final protected function FireEventBeforeDatabaseCreate()
{
$this->FireEvent(EVENT_DB_BEFORE_CREATE);
}
/**
* @return void
* @throws \CoreException
@@ -5894,6 +5899,16 @@ JS
/// UPDATE
///
/**
* @return void
* @throws \CoreException
*/
final protected function FireEventBeforeDatabaseUpdate()
{
$this->FireEvent(EVENT_DB_BEFORE_UPDATE);
}
/**
* @param array $aChanges
*

View File

@@ -203,6 +203,23 @@
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_BEFORE_CREATE" _delta="define">
<description>An object is about to be created into the database. The object can be modified.</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>
</sources>
<replaces>DBObject::OnInsert</replaces>
<event_data>
<event_datum id="object">
<description>The object inserted</description>
<type>DBObject</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
<type>string</type>
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_CREATE_DONE" _delta="define">
<description>An object has been created into the database. The modifications can be propagated to other objects.</description>
<sources>
@@ -220,6 +237,23 @@
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_BEFORE_UPDATE" _delta="define">
<description>An object is about to be updated into the database. The object can be modified.</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>
</sources>
<replaces>DBObject::OnUpdate</replaces>
<event_data>
<event_datum id="object">
<description>The object updated</description>
<type>DBObject</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
<type>string</type>
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_UPDATE_DONE" _delta="define">
<description>An object has been updated into the database and reloaded.</description>
<sources>

View File

@@ -3086,6 +3086,8 @@ abstract class DBObject implements iDisplay
$this->ComputeStopWatchesDeadline(true);
$this->FireEventBeforeDatabaseCreate();
$iTransactionRetry = 1;
$bIsTransactionEnabled = MetaModel::GetConfig()->Get('db_core_transactions_enabled');
if ($bIsTransactionEnabled) {
@@ -3276,6 +3278,8 @@ abstract class DBObject implements iDisplay
$this->ComputeStopWatchesDeadline(false);
$this->OnUpdate();
$this->FireEventBeforeDatabaseUpdate();
// Freeze the changes at this point
$this->InitPreviousValuesForUpdatedAttributes();
$aChanges = $this->ListChanges();
@@ -6053,6 +6057,14 @@ abstract class DBObject implements iDisplay
{
}
/**
* @return void
* @since 3.1.0
*/
protected function FireEventBeforeDatabaseCreate()
{
}
/**
* @return void
* @since 3.1.0
@@ -6069,6 +6081,16 @@ abstract class DBObject implements iDisplay
* @return void
* @since 3.1.0
*/
protected function FireEventBeforeDatabaseUpdate()
{
}
/**
* @param array $aChanges
*
* @return void
* @since 3.1.0
*/
protected function FireEventUpdateDone(array $aChanges): void
{
}
@@ -6078,6 +6100,8 @@ abstract class DBObject implements iDisplay
///
/**
* @param \DeletionPlan $oDeletionPlan
*
* @return void
* @since 3.1.0
*/

View File

@@ -340,7 +340,7 @@ final class EventService
foreach (self::$aEventDescriptions as $sEvent => $aEventInfo) {
if (is_array($aEventInfo['description']->GetEventSources())) {
foreach ($aEventInfo['description']->GetEventSources() as $sSource) {
if (\MetaModel::IsValidClass($sSource) && ($sClass == $sSource || $oClass->isSubclassOf($sSource))) {
if (class_exists($sSource) && ($sClass == $sSource || $oClass->isSubclassOf($sSource))) {
$aRes[$sEvent] = $aEventInfo;
}
}