From 9e9565b9b29696bae8f74e02b0457208380caaef Mon Sep 17 00:00:00 2001 From: David Flatz Date: Mon, 8 Oct 2018 15:23:07 +0200 Subject: handle setting a dyncol to null by removing the field in dyncols --- it_dbi.class | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'it_dbi.class') diff --git a/it_dbi.class b/it_dbi.class index d9688cd..004c22d 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -264,8 +264,12 @@ function _set($tags, $force = false) { if (substr($field, 0, 1) === "-") $newdyns[] = "'\$.$f', " . $value; - else if ($force || isset($value) && isset($dyndata[$f]) ? strval($value) !== strval($dyndata[$f]) : $value !== $dyndata[$f] || !array_key_exists($f, $dyndata)) - $newdyns[] = "'\$.$f', " . $this->escape_string($value); + else if ($force || isset($value) && isset($dyndata[$f]) ? strval($value) !== strval($dyndata[$f]) : $value !== $dyndata[$f] || !array_key_exists($f, $dyndata)) { + if (is_null($value)) + $deldyns[] = "'\$.$f'"; + else + $newdyns[] = "'\$.$f', " . $this->escape_string($value); + } $alldyns[] = $this->escape_string($f) . ", " . (substr($field, 0, 1) === "-" ? $value : $this->escape_string($value)); $dyndata[$f] = $value; @@ -280,8 +284,14 @@ function _set($tags, $force = false) { if ($force == "insert") # INSERT/REPLACE $result[] = "dyncols = JSON_OBJECT(" . join(", ", (array)$alldyns) . ")"; - else if ($newdyns) - $result[] = "dyncols = JSON_SET(" . ($this->_dyndata ? 'dyncols' : '"{}"') . ", " . join(", ", $newdyns) .")"; + else if ($newdyns || $deldyns) { + $source = $this->_dyndata ? 'dyncols' : '"{}"'; + if ($newdyns) + $source = "JSON_SET($source, " . join(', ', $newdyns) . ')'; + if ($deldyns) + $source = "JSON_REMOVE($source, " . join(', ', $deldyns) . ')'; + $result[] = "dyncols = $source"; + } } $this->_writes += $result ? 1 : 0; -- cgit v1.2.3