From 9128768b99666c1c63f128b2d57778df3f738f0e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 29 Sep 2023 20:47:13 +0800 Subject: [PATCH] Fix issues found during testing. #762 --- src/Command/GetJobOrderArchiveDataCommand.php | 96 ++++++++++--------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/Command/GetJobOrderArchiveDataCommand.php b/src/Command/GetJobOrderArchiveDataCommand.php index e5ac289d..1ff9ac6d 100644 --- a/src/Command/GetJobOrderArchiveDataCommand.php +++ b/src/Command/GetJobOrderArchiveDataCommand.php @@ -106,6 +106,7 @@ class GetJobOrderArchiveDataCommand extends Command $archive_files = []; $jo_id_list = []; + $ii_id_list = []; while ($row = $results->fetchAssociative()) { @@ -115,6 +116,24 @@ class GetJobOrderArchiveDataCommand extends Command $jo_id_list[] = $row['id']; } + // write the array into the file + $file = $this->createDataFileRelatedArchiveData($jo_data, $jo_tname, $year, 'w'); + + if ($file != null) + { + $archive_files[$jo_tname] = $file; + } + + // error_log('jo_data total ' . count($jo_data['job_order'])); + // error_log('jo id list total ' . count($jo_id_list)); + + unset($jo_data); + + // load the job order archive file for job order into the database + $this->loadArchiveFiles($archive_files, $year); + + unset($archive_files[$jo_tname]); + // get all related data for job order foreach ($jo_id_list as $jo_id) { @@ -155,26 +174,16 @@ class GetJobOrderArchiveDataCommand extends Command } } - // write the array into the file - $file = $this->createDataFileRelatedArchiveData($jo_data, $jo_tname, $year, 'w'); - - if ($file != null) - { - $archive_files[$jo_tname] = $file; - } - - // error_log(print_r($archive_files, true)); + // TODO: can we maybe find a way to not load the archive files successively? + // we get the memory exception after loading the data to the last table, jo_rejection. $this->loadArchiveFiles($archive_files, $year); - // need to get the list of invoice ids for deletion for invoice items - $invoice_id_list = $this->getInvoiceIds($jo_id_list); - // update rider's active_jo_id and current_jo_id to null // so we can delete the old job orders (foreign key constraint) $this->updateRiderJobOrders($jo_id_list); // at this point, all the job order and related data have been archived into the database - $this->deleteData($jo_id_list, $invoice_id_list, $related_tables); + $this->deleteData($jo_id_list, $related_tables); } protected function getRelatedArchiveData($row, $query_stmt, $table_name, $year) @@ -207,6 +216,7 @@ class GetJobOrderArchiveDataCommand extends Command } // get the invoice items for archiving + $ii_id_list = []; foreach ($invoice_id_list as $i_id) { $ii_file = $this->getInvoiceItemArchiveData($i_id, $year); @@ -217,6 +227,8 @@ class GetJobOrderArchiveDataCommand extends Command // write the array into the file $file = $this->createDataFileRelatedArchiveData($data, $table_name, $year, 'a'); + unset($data); + if ($file != null) $files[$table_name] = $file; @@ -236,6 +248,7 @@ class GetJobOrderArchiveDataCommand extends Command $results = $query_stmt->executeQuery(); $ii_data = []; + $ii_id_list = []; while ($ii_row = $results->fetchAssociative()) { $id = $ii_row['id']; @@ -253,10 +266,19 @@ class GetJobOrderArchiveDataCommand extends Command $price, $battery_id ]; + + $ii_id_list[] = $id; } $file = $this->createDataFileRelatedArchiveData($ii_data, 'invoice_item', $year, 'a'); + unset($ii_data); + + // special case, delete the invoice items already + // so that we don't have to query for the invoice items to delete + if (count($ii_id_list) > 0) + $this->deleteInvoiceItems($ii_id_list); + return $file; } @@ -778,17 +800,22 @@ class GetJobOrderArchiveDataCommand extends Command } } - protected function deleteData($jo_id_list, $invoice_id_list, $related_tables) + protected function deleteInvoiceItems($ii_id_list) { $db = $this->em->getConnection(); // delete the invoice items first - $inv_ids = str_repeat('?,', count($invoice_id_list) - 1) . '?'; + $ii_ids = str_repeat('?,', count($ii_id_list) - 1) . '?'; - $ii_del_sql = 'DELETE FROM invoice_item WHERE invoice_id IN (' . $inv_ids . ')'; + $ii_del_sql = 'DELETE FROM invoice_item WHERE id IN (' . $ii_ids . ')'; $ii_stmt = $db->prepare($ii_del_sql); - $ii_stmt->execute($invoice_id_list); + $ii_stmt->execute($ii_id_list); + } + + protected function deleteData($jo_id_list, $related_tables) + { + $db = $this->em->getConnection(); // delete from invoice, jo_rejection, rider_rating, ticket, and jo_event $jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?'; @@ -821,36 +848,8 @@ class GetJobOrderArchiveDataCommand extends Command $jo_stmt->execute($jo_id_list); } - protected function getInvoiceIds($jo_id_list) - { - $invoice_id_list = []; - - $db = $this->em->getConnection(); - $db->getWrappedConnection()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); - - $query_sql = 'SELECT id FROM invoice WHERE job_order_id = :id'; - - $query_stmt = $db->prepare($query_sql); - - foreach ($jo_id_list as $jo_id) - { - // need to get the invoice ids for the invoice items to delete - $query_stmt->bindValue('id', $jo_id); - - $results = $query_stmt->executeQuery(); - - while ($row = $results->fetchAssociative()) - { - $invoice_id_list[] = $row['id']; - } - } - - return $invoice_id_list; - } - protected function updateRiderJobOrders($jo_id_list) { - // TODO: test this $db = $this->em->getConnection(); $jo_ids = str_repeat('?,', count($jo_id_list) - 1) . '?'; @@ -859,17 +858,20 @@ class GetJobOrderArchiveDataCommand extends Command $update_curr_stmt = $db->prepare($update_curr_sql); $update_active_sql = 'UPDATE rider SET active_jo_id = NULL WHERE active_jo_id IN (' . $jo_ids . ')'; - $udpate_active_stmt = $db->prepare($update_active_sql); + $update_active_stmt = $db->prepare($update_active_sql); $update_curr_stmt->execute($jo_id_list); $update_active_stmt->execute($jo_id_list); + + unset($update_curr_stmt); + unset($update_active_stmt); } protected function loadDataFileForArchiveData($load_stmt) { $conn = $this->em->getConnection(); + $conn->getWrappedConnection()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); - // this statement is for job order $stmt = $conn->prepare($load_stmt); $result = $stmt->execute();