DbObject: fix multicolumn primary keys

This commit is contained in:
Alexander Fuhr 2015-06-02 17:27:36 +02:00
parent a991e6815d
commit 43982b6c3a

View File

@ -426,10 +426,21 @@ abstract class DbObject
public function getId() public function getId()
{ {
// TODO: Doesn't work for array() / multicol key // TODO: Doesn't work for array() / multicol key
if (is_array($this->keyName)) {
$id = array();
foreach ($this->keyName as $key) {
if (! isset($this->properties[$key])) {
return null; // Really?
}
$id[$key] = $this->properties[$key];
}
return $id;
} else {
if (isset($this->properties[$this->keyName])) if (isset($this->properties[$this->keyName]))
{ {
return $this->properties[$this->keyName]; return $this->properties[$this->keyName];
} }
}
return null; return null;
} }
@ -633,8 +644,8 @@ abstract class DbObject
{ {
$keyname = $this->getKeyName(); $keyname = $this->getKeyName();
if (is_array($keyname)) { if (is_array($keyname)) {
foreach ($keyname as $idx => $k) { foreach ($keyname as $k) {
$this->set($k, $key[$idx]); $this->set($k, $key[$k]);
} }
} else { } else {
$this->set($keyname, $key); $this->set($keyname, $key);
@ -653,14 +664,21 @@ abstract class DbObject
protected function createWhere() protected function createWhere()
{ {
$key = $this->getKeyName(); $key = $this->getKeyName();
if (is_array($key) && ! is_empty($key)) { if (is_array($key) && ! empty($key)) {
$where = array(); $where = array();
foreach ($key as $k) { foreach ($key as $k) {
if ($this->hasBeenLoadedFromDb()) {
$where[] = $this->db->quoteInto(
sprintf('%s = ?', $k),
$this->loadedProperties[$k]
);
} else {
$where[] = $this->db->quoteInto( $where[] = $this->db->quoteInto(
sprintf('%s = ?', $k), sprintf('%s = ?', $k),
$this->properties[$k] $this->properties[$k]
); );
} }
}
return implode(' AND ', $where); return implode(' AND ', $where);
} else { } else {
return $this->db->quoteInto( return $this->db->quoteInto(