Database transaction is not working as expected even rollback is not working

41 Views Asked by At

i have written a code on insert into backup and then deletion from main table but i had applied transaction but it is not working even if any error occurs in between transaction then data without any cause gets deleted without backup.


public function deletedata($DeletedBy, $ID, $comment){
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $db->beginTransaction();
        
        try{
            $IDsArray = explode(',', $ID);
                $kmr_member_delete_table_record = "SELECT TableName, UseridIdentification FROM kmr_member_delete_table_record WHERE `IsDeleted` = 0 ORDER BY TableName ASC";
                
                $member_delete_table_record = $db->query($kmr_member_delete_table_record)->fetchAll();
                foreach ($IDsArray as $individualID) {
                    
                    $this->InsertMemberData($db, $individualID, $member_delete_table_record,$DeletedBy,$comment);
                    
                }
                $db->commit();
                
        }catch(Zend_Db_Exception $e){
            $db->rollBack();
            throw new Exception($e->getMessage());
        }
    }

private function InsertMemberData($db, $individualID, $member_delete_table_record,$DeletedBy,$comment){
        
        foreach($member_delete_table_record as $value){
            
            $table_name = $value["TableName"];
            $UseridIdentification = $value["UseridIdentification"];
            
            $selectTableData = "SELECT * FROM " . $table_name . " WHERE " . $UseridIdentification . " = '".$individualID."';";
            $result1 = $db->query($selectTableData)->fetchAll();
            $jsondata_Deleted_Data = json_encode($result1);
            
            $LASTNAME = "";
            $FIRSTNAME = "";
            $MIDDLEINITIAL = "";
            $SSN = "";
            $CardClientNo = "";
            $STATUS = "";
            $CLASS = "";
            $UnionRateClassification = "";
            $ApprenticeClassLevel = "";
            
            if (!empty($result1)) {
                
                $details_user_static_report = "SELECT * FROM `kmr_user_static_report` WHERE `UserID` = '".$individualID."';";
                $details_data_user_static_report = $db->query($details_user_static_report)->fetchAll();
                foreach($details_data_user_static_report as $res){
                    $LASTNAME = $res["LASTNAME"];
                    $FIRSTNAME = $res["FIRSTNAME"];
                    $MIDDLEINITIAL = $res["MIDDLEINITIAL"];
                    $SSN = $res["SSN"];
                    $STATUS = $res["STATUS"];
                    $CLASS = $res["CLASS"];
                    $UnionRateClassification = $res["UnionRateClassification"];
                    $ApprenticeClassLevel = $res["ApprenticeClassLevel"];
                    if($res["CardClientNo"] != "")
                        $CardClientNo = $res["CardClientNo"];
                    else
                        $CardClientNo = "";
                }
                
                $details_data = array(
                        'UserId' => $individualID,
                        'Deleted_Table_Name' => $table_name,
                        'Deleted_Data' => $jsondata_Deleted_Data,
                        'LASTNAME' => $LASTNAME,
                        'FIRSTNAME' => $FIRSTNAME,
                        'MIDDLEINITIAL' => $MIDDLEINITIAL,
                        'SSN' => $SSN,
                        'CardClientNo' => $CardClientNo,
                        'STATUS' => $STATUS,
                        'CLASS' => $CLASS,
                        'UnionRateClassification' => $UnionRateClassification,
                        'ApprenticeClassLevel' => $ApprenticeClassLevel,
                        'Deleted_By' => $DeletedBy,
                        'Deleted_Datetime' => date("Y-m-d H:i:s"),
                        'Deleted_reason' => $comment,
                        'is_reverted' => 1
                );
                $db->insert('kmr_member_deleted_archieve', $details_data);
                $delete1_member_details = "DELETE FROM " . $table_name . " WHERE " . $UseridIdentification . " = '".$individualID."';";
                $db->query($delete1_member_details);
            }
        }
        
    }
0

There are 0 best solutions below