From d055c68a5c025c35dbd91d675facac4d958559b6 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 10 Dec 2015 11:42:21 +0100 Subject: [PATCH] DbObject: allow null components in multicol keys --- library/Director/Data/Db/DbObject.php | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index c2474bb5..758a8744 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -724,11 +724,8 @@ abstract class DbObject } foreach ($keyname as $k) { if (! array_key_exists($k, $key)) { - throw new IE( - 'Required key component "%s" is missing for "%s", got %s', - $k, - json_encode($key) - ); + // We allow for null in multicolumn keys: + $key[$k] = null; } $this->set($k, $key[$k]); } @@ -756,21 +753,31 @@ abstract class DbObject } $key = $this->getKeyName(); + if (is_array($key) && ! empty($key)) { $where = array(); foreach ($key as $k) { if ($this->hasBeenLoadedFromDb()) { - $where[] = $this->db->quoteInto( - sprintf('%s = ?', $k), - $this->loadedProperties[$k] - ); + if ($this->loadedProperties[$k] === null) { + $where[] = sprintf('%s IS NULL', $k); + } else { + $where[] = $this->db->quoteInto( + sprintf('%s = ?', $k), + $this->loadedProperties[$k] + ); + } } else { - $where[] = $this->db->quoteInto( - sprintf('%s = ?', $k), - $this->properties[$k] - ); + if ($this->properties[$k] === null) { + $where[] = sprintf('%s IS NULL', $k); + } else { + $where[] = $this->db->quoteInto( + sprintf('%s = ?', $k), + $this->properties[$k] + ); + } } } + return implode(' AND ', $where); } else { if ($this->hasBeenLoadedFromDb()) {