From 8ca79b665761a8b15cd2a9415fb2a2b3f101e159 Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 16 Nov 2017 16:27:14 +0100 Subject: [PATCH] Changed method getOERoot of MessageToken_model to retrive the correct root of the organisation unit tree --- .../models/system/MessageToken_model.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/application/models/system/MessageToken_model.php b/application/models/system/MessageToken_model.php index 8c31a0807..9e62af6e9 100644 --- a/application/models/system/MessageToken_model.php +++ b/application/models/system/MessageToken_model.php @@ -215,7 +215,23 @@ class MessageToken_model extends CI_Model */ public function getOERoot($oe_kurzbz) { - $sql = 'SELECT oe_kurzbz, oe_parent_kurzbz FROM public.tbl_organisationseinheit WHERE oe_kurzbz = ?'; + $sql = ' + WITH RECURSIVE organizations(rid, oe_kurzbz, oe_parent_kurzbz) AS + ( + SELECT 1 AS rid, o.oe_kurzbz, o.oe_parent_kurzbz + FROM public.tbl_organisationseinheit o + WHERE o.oe_kurzbz = ? + UNION ALL + SELECT rid + 1 AS rid, oe.oe_kurzbz, oe.oe_parent_kurzbz + FROM organizations org, public.tbl_organisationseinheit oe + WHERE oe.oe_kurzbz = org.oe_parent_kurzbz + AND oe.aktiv = TRUE + ) + SELECT oe_kurzbz + FROM organizations orgs + ORDER BY rid DESC + LIMIT 1 + '; $result = $this->db->query($sql, array($oe_kurzbz)); if ($result) // If no errors occurred