diff options
author | Simon Holywell <[email protected]> | 2014-06-21 23:41:46 +0100 |
---|---|---|
committer | Simon Holywell <[email protected]> | 2014-06-21 23:41:46 +0100 |
commit | 6dbd87954c5be02d69259e3bc236081f8cd34310 (patch) | |
tree | bce68c265f66dcebbc806b8c27985415b2be7671 /idiorm.php | |
parent | b616165902f383f068eba25f3c979005fe0d4148 (diff) | |
parent | 90469df2980ff338e91fb23a514d24a135e3147b (diff) |
Merge pull request #205 from cainmi/develop
Restrict null primary keys on update/delete, resolves #203
Diffstat (limited to 'idiorm.php')
-rw-r--r-- | idiorm.php | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -1879,8 +1879,22 @@ /** * Get the primary key ID of this object. */ - public function id() { - return $this->get($this->_get_id_column_name()); + public function id($disallow_null = false) { + $id = $this->get($this->_get_id_column_name()); + + if ($disallow_null) { + if (is_array($id)) { + foreach ($id as $id_part) { + if ($id_part === null) { + throw new Exception('Primary key ID contains null value(s)'); + } + } + } else if ($id === null) { + throw new Exception('Primary key ID missing from row or is null'); + } + } + + return $id; } /** @@ -1961,7 +1975,7 @@ return true; } $query = $this->_build_update(); - $id = $this->id(); + $id = $this->id(true); if (is_array($id)) { $values = array_merge($values, array_values($id)); } else { @@ -2068,7 +2082,7 @@ $this->_quote_identifier($this->_table_name) ); $this->_add_id_column_conditions($query); - return self::_execute(join(" ", $query), is_array($this->id()) ? array_values($this->id()) : array($this->id()), $this->_connection_name); + return self::_execute(join(" ", $query), is_array($this->id(true)) ? array_values($this->id(true)) : array($this->id(true)), $this->_connection_name); } /** |