DbObject: fix multicolumn primary keys

This commit is contained in:
Alexander Fuhr 2015-06-02 17:27:36 +02:00
parent a991e6815d
commit 43982b6c3a
1 changed files with 29 additions and 11 deletions

View File

@ -426,9 +426,20 @@ 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 (isset($this->properties[$this->keyName])) if (is_array($this->keyName)) {
{ $id = array();
return $this->properties[$this->keyName]; 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]))
{
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,13 +664,20 @@ 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) {
$where[] = $this->db->quoteInto( if ($this->hasBeenLoadedFromDb()) {
sprintf('%s = ?', $k), $where[] = $this->db->quoteInto(
$this->properties[$k] sprintf('%s = ?', $k),
); $this->loadedProperties[$k]
);
} else {
$where[] = $this->db->quoteInto(
sprintf('%s = ?', $k),
$this->properties[$k]
);
}
} }
return implode(' AND ', $where); return implode(' AND ', $where);
} else { } else {