diff options
-rw-r--r-- | idiorm.php | 22 | ||||
-rw-r--r-- | test/ORMTest.php | 68 |
2 files changed, 86 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); } /** diff --git a/test/ORMTest.php b/test/ORMTest.php index e0e4900..3df3a73 100644 --- a/test/ORMTest.php +++ b/test/ORMTest.php @@ -99,4 +99,72 @@ class ORMTest extends PHPUnit_Framework_TestCase { $resultSet = ORM::for_table('test')->find_result_set(); $resultSet->invalidFunctionCall(); } + + /** + * @expectedException Exception + * @expectedExceptionMessage Primary key ID missing from row or is null + * These next two tests are needed because if you have select()ed some fields, + * but not the primary key, then the primary key is not available for the + * update/delete query - see issue #203. + * We need to change the primary key here to something other than `id` + * becuase MockPDOStatement->fetch() always returns an id. + */ + public function testUpdateNullPrimaryKey() { + $widget = ORM::for_table('widget') + ->use_id_column('primary') + ->select('foo') + ->where('primary', 1) + ->find_one() + ; + + $widget->foo = 'bar'; + $widget->save(); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Primary key ID missing from row or is null + */ + public function testDeleteNullPrimaryKey() { + $widget = ORM::for_table('widget') + ->use_id_column('primary') + ->select('foo') + ->where('primary', 1) + ->find_one() + ; + + $widget->delete(); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Primary key ID missing from row or is null + */ + public function testNullPrimaryKey() { + $widget = ORM::for_table('widget') + ->use_id_column('primary') + ->select('foo') + ->where('primary', 1) + ->find_one() + ; + + $widget->id(true); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Primary key ID contains null value(s) + */ + public function testNullPrimaryKeyPart() { + $widget = ORM::for_table('widget') + ->use_id_column(array('id', 'primary')) + ->select('foo') + ->where('id', 1) + ->where('primary', 1) + ->find_one() + ; + + $widget->id(true); + + } }
\ No newline at end of file |