From 4fccb57e7a25316ae74e0ac9db04b87626a3416f Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 16 Nov 2017 13:13:22 +0000 Subject: [PATCH] dblayeroo: allow the foreign keys to be a NOT NULL field, then not be tested git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4002 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayeroo.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/dblayeroo.php b/dblayeroo.php index 0740993..5129133 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -2726,7 +2726,8 @@ class dblayeroo // recorded if ($update === true && ! array_key_exists ($field, $values)) continue 2; - if (! array_key_exists ($field, $values)) + if (! array_key_exists ($field, $values) && + in_array ("not null", $this->fields[$field])) { $errors[$field] = sprintf (dgettext ("domframework", "The field '%s' must be test on foreign, but is not provided"), @@ -2737,6 +2738,9 @@ class dblayeroo $this->DBException (sprintf (dgettext ("domframework", "No foreign object configured to test the foreign key for table ". "'%s'"), $this->table)); + if (! array_key_exists ($field, $values) && + ! in_array ("not null", $this->fields[$field])) + $values[$field] = null; } $this->debugLog ("CLONE to check foreign constraint [$fields]"); $objTmp = clone $this->setForeignObj[$params[0]]; @@ -2746,8 +2750,21 @@ class dblayeroo $objTmp->Select (); $objTmp->displayAdd ($objTmp->primary); $parentField = explode (",", $params[1]); + $i = 0; foreach (explode (",", $fields) as $key=>$field) - $objTmp->whereAdd ($parentField[$key], "=", $values[$field]); + { + if ($values[$field] !== null) + { + $objTmp->whereAdd ($parentField[$key], "=", $values[$field]); + $i++; + } + } + if ($i == 0) + { + // If the foreign is null, do not test as there is no WHERE clause, and + // will return all the table + continue; + } if (count ($objTmp->execute ()) === 0) $errors[$field] = sprintf (dgettext ("domframework", "The value of the foreign key '%s' doesn't exists in foreign table"),