/** * @param \obo\Entity $entity * @param bool $forced * @param bool $triggerEvents * @return void * @throws \obo\Exceptions\EntityIsDeletedException * @throws \obo\Exceptions\EntityIsNotInitializedException * @throws \obo\Exceptions\Exception * @throws \obo\Exceptions\ServicesException */ public static function saveEntity(\obo\Entity $entity, $forced = false, $triggerEvents = true) { if (!$entity->isInitialized()) { throw new \obo\Exceptions\EntityIsNotInitializedException("Cannot save entity which is not initialized"); } if (!$forced and $entity->isDeleted() and !$entity->isDeletingInProgress()) { throw new \obo\Exceptions\EntityIsDeletedException("Cannot save entity which is deleted"); } if ($entity->entityInformation()->repositoryName === null) { throw new \obo\Exceptions\Exception("Entity '" . $entity->className() . "' cannot be persisted. No entity storage exists"); } $entity->setSavingInProgress(); if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeSave", $entity); } if (count($entity->changedProperties($entity->entityInformation()->persistablePropertiesNames, true, true))) { if ($entity->isBasedInRepository()) { if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeUpdate", $entity); } $entity->dataStorage()->updateEntity($entity); $entity->markUnpersistedPropertiesAsPersisted(); if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterSave", $entity); } if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterUpdate", $entity); } } else { if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeInsert", $entity); } $entity->dataStorage()->insertEntity($entity); $entity->setBasedInRepository(true); $entity->markUnpersistedPropertiesAsPersisted(); if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterSave", $entity); } if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterInsert", $entity); } } } else { if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterSave", $entity); } } $entity->setSavingInProgress(false); }