From ee989de98829ad458aceff7d1ec7371c59f108b3 Mon Sep 17 00:00:00 2001 From: HB9HIL Date: Sat, 16 Dec 2023 09:37:45 +0100 Subject: [PATCH] added logic to auto-populate the hrd_username from station_callsign --- .../migrations/162_hrdlog_username.php | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/application/migrations/162_hrdlog_username.php b/application/migrations/162_hrdlog_username.php index bdfab65c..51b71d13 100644 --- a/application/migrations/162_hrdlog_username.php +++ b/application/migrations/162_hrdlog_username.php @@ -1,14 +1,14 @@ db->field_exists('hrdlog_username', 'station_profile')) { @@ -17,6 +17,23 @@ class Migration_hrdlog_username extends CI_Migration { ); $this->dbforge->add_column('station_profile', $fields); } + + // SELECT all rows where hrdlog_code is not empty + $this->db->where("(hrdlog_code IS NOT NULL AND hrdlog_code != '')"); + $query = $this->db->get('station_profile'); + $rows = $query->result(); + + // Iterate through all selected rows + foreach ($rows as $row) { + // Extract the username using the regex pattern + $regex = '/^((\d|[A-Z])+\/)?((\d|[A-Z]){3,})(\/(\d|[A-Z])+)?(\/(\d|[A-Z])+)?$/'; + preg_match($regex, $row->station_callsign, $matches); + $username = $matches[3]; + + // Update the row with the extracted username + $this->db->where('station_id', $row->station_id); + $this->db->update('station_profile', array('hrdlog_username' => $username)); + } } public function down() @@ -25,4 +42,4 @@ class Migration_hrdlog_username extends CI_Migration { $this->dbforge->drop_column('station_profile', 'hrdlog_username'); } } -} \ No newline at end of file +}