public function testLogicalDelete() { class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::INCLUDED()); $intCountTotal = class_module_system_aspect::getObjectCount(); class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUDED()); $intCountActive = class_module_system_aspect::getObjectCount(); class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUSIVE()); $intCountDeleted = class_module_system_aspect::getObjectCount(); class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUDED()); echo "Creating aspect\n"; $objAspect1 = new class_module_system_aspect(); $objAspect1->setStrName("Dummy"); $objAspect1->updateObjectToDb(); $objAspect = new class_module_system_aspect(); $objAspect->setStrName("logical delete"); $objAspect->updateObjectToDb(); $strAspectId = $objAspect->getSystemid(); $this->assertEquals($intCountActive + 2, class_module_system_aspect::getObjectCount()); $arrAspects = class_module_system_aspect::getObjectList(); $arrAspects = array_filter($arrAspects, function (class_module_system_aspect $objAspect) use($strAspectId) { return $objAspect->getSystemid() == $strAspectId; }); $this->assertEquals(1, count($arrAspects)); echo "Deleting logically\n"; $this->assertEquals($objAspect->getIntRecordDeleted(), 0); $objAspect->deleteObject(); $this->assertEquals($objAspect->getIntRecordDeleted(), 1); $this->assertEquals($objAspect->getIntSort(), -1); class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUDED()); echo "Loading non-deleted only\n"; $this->assertEquals($intCountActive + 1, class_module_system_aspect::getObjectCount()); $arrAspects = class_module_system_aspect::getObjectList(); $arrAspects = array_filter($arrAspects, function (class_module_system_aspect $objAspect) use($strAspectId) { return $objAspect->getSystemid() == $strAspectId; }); $this->assertEquals(0, count($arrAspects)); echo "Loading deleted only\n"; class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUSIVE()); $arrAspects = class_module_system_aspect::getObjectList(); $arrAspects = array_filter($arrAspects, function (class_module_system_aspect $objAspect) use($strAspectId) { return $objAspect->getSystemid() == $strAspectId; }); $this->assertEquals($intCountDeleted + 1, class_module_system_aspect::getObjectCount()); $this->assertEquals(1, count($arrAspects)); echo "Loading mixed deleted and non-deleted\n"; class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::INCLUDED()); $arrAspects = class_module_system_aspect::getObjectList(); $arrAspects = array_filter($arrAspects, function (class_module_system_aspect $objAspect) use($strAspectId) { return $objAspect->getSystemid() == $strAspectId; }); $this->assertEquals($intCountTotal + 2, class_module_system_aspect::getObjectCount()); $this->assertEquals(1, count($arrAspects)); echo "Deleting from database\n"; $objAspect->deleteObjectFromDatabase(); class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUDED()); echo "Loading non-deleted only\n"; $this->assertEquals($intCountActive + 1, class_module_system_aspect::getObjectCount()); $arrAspects = class_module_system_aspect::getObjectList(); $arrAspects = array_filter($arrAspects, function (class_module_system_aspect $objAspect) use($strAspectId) { return $objAspect->getSystemid() == $strAspectId; }); $this->assertEquals(0, count($arrAspects)); echo "Loading deleted only\n"; class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUSIVE()); $arrAspects = class_module_system_aspect::getObjectList(); $arrAspects = array_filter($arrAspects, function (class_module_system_aspect $objAspect) use($strAspectId) { return $objAspect->getSystemid() == $strAspectId; }); $this->assertEquals($intCountDeleted, class_module_system_aspect::getObjectCount()); $this->assertEquals(0, count($arrAspects)); echo "Deleting dummy node directly\n"; $objAspect1->deleteObjectFromDatabase(); echo "Loading non-deleted only\n"; class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUDED()); $this->assertEquals($intCountActive, class_module_system_aspect::getObjectCount()); echo "Loading deleted only\n"; class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUSIVE()); $this->assertEquals($intCountDeleted, class_module_system_aspect::getObjectCount()); }
/** * Generates the where restriction for queries, based on the current config. * Currently the methods returns a string based where restriction. * * @param string $strSystemTablePrefix * @param string $strConjunction * @return string */ public function getDeletedWhereRestriction($strSystemTablePrefix = "", $strConjunction = "AND") { $strQuery = ""; if ($strSystemTablePrefix != "") { $strSystemTablePrefix = $strSystemTablePrefix . "."; } if (self::$bitLogcialDeleteAvailable) { if ($this->getIntCombinedLogicalDeletionConfig()->equals(class_orm_deletedhandling_enum::EXCLUDED())) { $strQuery .= " " . $strConjunction . " {$strSystemTablePrefix}system_deleted = 0 "; } else { if ($this->getIntCombinedLogicalDeletionConfig()->equals(class_orm_deletedhandling_enum::EXCLUSIVE())) { $strQuery .= " " . $strConjunction . " {$strSystemTablePrefix}system_deleted = 1 "; } } } return $strQuery; }
/** * Triggers the update sequence for assignment properties * @return bool */ private function updateAssignments() { $bitReturn = true; $objReflection = new class_reflection($this->getObjObject()); //get the mapped properties $arrProperties = $objReflection->getPropertiesWithAnnotation(class_orm_base::STR_ANNOTATION_OBJECTLIST, class_reflection_enum::PARAMS()); foreach ($arrProperties as $strPropertyName => $arrValues) { $objCfg = class_orm_assignment_config::getConfigForProperty($this->getObjObject(), $strPropertyName); //try to load the orm config of the arrayObject - if given $strGetter = $objReflection->getGetter($strPropertyName); $arrValues = null; if ($strGetter !== null) { $arrValues = call_user_func(array($this->getObjObject(), $strGetter)); } $objAssignmentDeleteHandling = $this->getIntCombinedLogicalDeletionConfig(); if ($arrValues != null && $arrValues instanceof class_orm_assignment_array) { $objAssignmentDeleteHandling = $arrValues->getObjDeletedHandling(); } //try to restore the object-set from the database using the same config as when initializing the object $objOldHandling = $this->getIntCombinedLogicalDeletionConfig(); $this->setObjHandleLogicalDeleted($objAssignmentDeleteHandling); $arrAssignmentsFromObject = $this->getAssignmentValuesFromObject($strPropertyName, $objCfg->getArrTypeFilter()); $arrAssignmentsFromDatabase = $this->getAssignmentsFromDatabase($strPropertyName); $this->setObjHandleLogicalDeleted($objOldHandling); //if the delete handling was set to excluded when loading the assignment, the logically deleted nodes should be merged with the values from db if ($objAssignmentDeleteHandling->equals(class_orm_deletedhandling_enum::EXCLUDED())) { $this->setObjHandleLogicalDeleted(class_orm_deletedhandling_enum::EXCLUSIVE()); $arrDeletedIds = $this->getAssignmentsFromDatabase($strPropertyName); $this->setObjHandleLogicalDeleted($objOldHandling); foreach ($arrDeletedIds as $strOneId) { if (!in_array($strOneId, $arrAssignmentsFromDatabase)) { $arrAssignmentsFromDatabase[] = $strOneId; } if (!in_array($strOneId, $arrAssignmentsFromObject)) { $arrAssignmentsFromObject[] = $strOneId; } } } sort($arrAssignmentsFromObject); sort($arrAssignmentsFromDatabase); //only do s.th. if the array differs $arrNewAssignments = array_diff($arrAssignmentsFromObject, $arrAssignmentsFromDatabase); $arrDeletedAssignments = array_diff($arrAssignmentsFromDatabase, $arrAssignmentsFromObject); //skip in case there's nothing to do if (count($arrNewAssignments) == 0 && count($arrDeletedAssignments) == 0) { continue; } $objDB = class_carrier::getInstance()->getObjDB(); $arrInserts = array(); foreach ($arrAssignmentsFromObject as $strOneTargetId) { $arrInserts[] = array($this->getObjObject()->getSystemid(), $strOneTargetId); } $bitReturn = $bitReturn && $objDB->_pQuery("DELETE FROM " . $objDB->encloseTableName(_dbprefix_ . $objCfg->getStrTableName()) . " WHERE " . $objDB->encloseColumnName($objCfg->getStrSourceColumn()) . " = ?", array($this->getObjObject()->getSystemid())); $bitReturn = $bitReturn && $objDB->multiInsert($objCfg->getStrTableName(), array($objCfg->getStrSourceColumn(), $objCfg->getStrTargetColumn()), $arrInserts); $bitReturn = $bitReturn && class_core_eventdispatcher::getInstance()->notifyGenericListeners(class_system_eventidentifier::EVENT_SYSTEM_OBJECTASSIGNMENTSUPDATED, array(array_values($arrNewAssignments), array_values($arrDeletedAssignments), array_values($arrAssignmentsFromObject), $this->getObjObject(), $strPropertyName)); if ($objReflection->hasPropertyAnnotation($strPropertyName, class_module_system_changelog::ANNOTATION_PROPERTY_VERSIONABLE)) { $objChanges = new class_module_system_changelog(); $objChanges->setOldValueForSystemidAndProperty($this->getObjObject()->getSystemid(), $strPropertyName, implode(",", $arrAssignmentsFromDatabase)); } } return $bitReturn; }
public function testLogicalDeleteLoadHandling() { $objDB = class_carrier::getInstance()->getObjDB(); /** @var orm_objectlist_testclass $objTestobject */ $objTestobject = $this->getObject("testobject"); $arrAspects = array($this->getObject("aspect1"), $this->getObject("aspect2"), $this->getObject("aspect3")); $objTestobject->setArrObject1($arrAspects); $objTestobject->updateObjectToDb(); $arrRow = $objDB->getPRow("SELECT COUNT(*) FROM " . _dbprefix_ . "testclass_rel WHERE testclass_source_id = ?", array($objTestobject->getSystemid())); $this->assertEquals(3, $arrRow["COUNT(*)"]); //delete one aspect logically $arrAspects[1]->deleteObject(); $objTestobject = new orm_objectlist_testclass($objTestobject->getSystemid()); $this->assertEquals(count($objTestobject->getArrObject1()), 2); class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::INCLUDED()); $objTestobject = new orm_objectlist_testclass($objTestobject->getSystemid()); class_orm_base::setObjHandleLogicalDeletedGlobal(null); $this->assertEquals(count($objTestobject->getArrObject1()), 3); class_orm_base::setObjHandleLogicalDeletedGlobal(class_orm_deletedhandling_enum::EXCLUSIVE()); $objTestobject = new orm_objectlist_testclass($objTestobject->getSystemid()); class_orm_base::setObjHandleLogicalDeletedGlobal(null); $this->assertEquals(count($objTestobject->getArrObject1()), 1); $this->assertEquals($objTestobject->getArrObject1()[0]->getSystemid(), $arrAspects[1]->getSystemid()); class_orm_base::setObjHandleLogicalDeletedGlobal(null); $objTestobject = new orm_objectlist_testclass($objTestobject->getSystemid()); class_orm_base::setObjHandleLogicalDeletedGlobal(null); $this->assertEquals(count($objTestobject->getArrObject1()), 2); $this->assertTrue(in_array($objTestobject->getArrObject1()[0]->getSystemid(), array($arrAspects[0]->getSystemid(), $arrAspects[2]->getSystemid()))); }