Browse Source

Saving an empty entity should fail.

Saving entity with no data is a failure as we cannot generate meaningful
SQL for this situation.
mark_story 12 years ago
parent
commit
015fb32691
2 changed files with 19 additions and 4 deletions
  1. 7 4
      src/ORM/Table.php
  2. 12 0
      tests/TestCase/ORM/TableTest.php

+ 7 - 4
src/ORM/Table.php

@@ -1189,10 +1189,9 @@ class Table implements EventListener {
 		$id = (array)$this->_newId($primary) + $keys;
 		$primary = array_combine($primary, $id);
 		$filteredKeys = array_filter($primary, 'strlen');
-		$total = count($primary);
 		$data = $filteredKeys + $data;
 
-		if ($total > 1) {
+		if (count($primary) > 1) {
 			foreach ($primary as $k => $v) {
 				if (!isset($data[$k])) {
 					$msg = 'Cannot insert row, some of the primary key values are missing. ';
@@ -1206,11 +1205,15 @@ class Table implements EventListener {
 			}
 		}
 
+		$success = false;
+		if (empty($data)) {
+			return $success;
+		}
+
 		$statement = $this->query()->insert(array_keys($data))
 			->values($data)
 			->execute();
 
-		$success = false;
 		if ($statement->rowCount() > 0) {
 			$success = $entity;
 			$entity->set($filteredKeys, ['guard' => false]);
@@ -1263,7 +1266,7 @@ class Table implements EventListener {
 
 		$filtered = array_filter($primaryKey, 'strlen');
 		if (count($filtered) < count($primaryKey)) {
-			$message = 'A primary key value is needed for updating';
+			$message = 'All primary key value(s) are needed for updating';
 			throw new \InvalidArgumentException($message);
 		}
 

+ 12 - 0
tests/TestCase/ORM/TableTest.php

@@ -1103,6 +1103,18 @@ class TableTest extends \Cake\TestSuite\TestCase {
 	}
 
 /**
+ * Test that saving a new empty entity does nothing.
+ *
+ * @group save
+ * @return void
+ */
+	public function testSaveNewEmptyEntity() {
+		$entity = new \Cake\ORM\Entity();
+		$table = TableRegistry::get('users');
+		$this->assertFalse($table->save($entity));
+	}
+
+/**
  * Tests that saving an entity will filter out properties that
  * are not present in the table schema when saving
  *