DbObject: allow null components in multicol keys

This commit is contained in:
Thomas Gelf 2015-12-10 11:42:21 +01:00
parent 46ce190813
commit d055c68a5c

View File

@ -724,11 +724,8 @@ abstract class DbObject
} }
foreach ($keyname as $k) { foreach ($keyname as $k) {
if (! array_key_exists($k, $key)) { if (! array_key_exists($k, $key)) {
throw new IE( // We allow for null in multicolumn keys:
'Required key component "%s" is missing for "%s", got %s', $key[$k] = null;
$k,
json_encode($key)
);
} }
$this->set($k, $key[$k]); $this->set($k, $key[$k]);
} }
@ -756,14 +753,22 @@ abstract class DbObject
} }
$key = $this->getKeyName(); $key = $this->getKeyName();
if (is_array($key) && ! empty($key)) { if (is_array($key) && ! empty($key)) {
$where = array(); $where = array();
foreach ($key as $k) { foreach ($key as $k) {
if ($this->hasBeenLoadedFromDb()) { if ($this->hasBeenLoadedFromDb()) {
if ($this->loadedProperties[$k] === null) {
$where[] = sprintf('%s IS NULL', $k);
} else {
$where[] = $this->db->quoteInto( $where[] = $this->db->quoteInto(
sprintf('%s = ?', $k), sprintf('%s = ?', $k),
$this->loadedProperties[$k] $this->loadedProperties[$k]
); );
}
} else {
if ($this->properties[$k] === null) {
$where[] = sprintf('%s IS NULL', $k);
} else { } else {
$where[] = $this->db->quoteInto( $where[] = $this->db->quoteInto(
sprintf('%s = ?', $k), sprintf('%s = ?', $k),
@ -771,6 +776,8 @@ abstract class DbObject
); );
} }
} }
}
return implode(' AND ', $where); return implode(' AND ', $where);
} else { } else {
if ($this->hasBeenLoadedFromDb()) { if ($this->hasBeenLoadedFromDb()) {