From d09ab6427f1ea7c3c46e91dabb4914b65b37bff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Ram=C3=B3n=20L=C3=B3pez?= Date: Thu, 2 Jan 2014 22:30:54 +0100 Subject: UPDATE queries are now compound primary keys aware --- idiorm.php | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/idiorm.php b/idiorm.php index 4e756f6..d0e98c8 100644 --- a/idiorm.php +++ b/idiorm.php @@ -1838,7 +1838,12 @@ return true; } $query = $this->_build_update(); - $values[] = $this->id(); + $id = $this->id(); + if (is_array($id)) { + $values = array_merge($values, array_values($id)); + } else { + $values[] = $id; + } } else { // INSERT $query = $this->_build_insert(); } @@ -1862,6 +1867,25 @@ return $success; } + /** + * Add a WHERE clause for every column that belongs to the primary key + */ + public function _add_id_column_conditions(&$query) { + $query[] = "WHERE"; + $keys = is_array($this->_get_id_column_name()) ? $this->_get_id_column_name() : array( $this->_get_id_column_name() ); + $first = true; + foreach($keys as $key) { + if ($first) { + $first = false; + } + else { + $query[] = "AND"; + } + $query[] = $this->_quote_identifier($key); + $query[] = "= ?"; + } + } + /** * Build an UPDATE query */ @@ -1877,9 +1901,7 @@ $field_list[] = "{$this->_quote_identifier($key)} = $value"; } $query[] = join(", ", $field_list); - $query[] = "WHERE"; - $query[] = $this->_quote_identifier($this->_get_id_column_name()); - $query[] = "= ?"; + $this->_add_id_column_conditions($query); return join(" ", $query); } -- cgit v1.2.3