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,21 +753,31 @@ 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()) {
$where[] = $this->db->quoteInto( if ($this->loadedProperties[$k] === null) {
sprintf('%s = ?', $k), $where[] = sprintf('%s IS NULL', $k);
$this->loadedProperties[$k] } else {
); $where[] = $this->db->quoteInto(
sprintf('%s = ?', $k),
$this->loadedProperties[$k]
);
}
} else { } else {
$where[] = $this->db->quoteInto( if ($this->properties[$k] === null) {
sprintf('%s = ?', $k), $where[] = sprintf('%s IS NULL', $k);
$this->properties[$k] } else {
); $where[] = $this->db->quoteInto(
sprintf('%s = ?', $k),
$this->properties[$k]
);
}
} }
} }
return implode(' AND ', $where); return implode(' AND ', $where);
} else { } else {
if ($this->hasBeenLoadedFromDb()) { if ($this->hasBeenLoadedFromDb()) {