Browse Source

Fixes SQLServer throwing a 'string not uuid' type error if a query is done with a empty value to a uuid field.

Walther Lalk 11 years ago
parent
commit
f60d099d77
2 changed files with 20 additions and 1 deletions
  1. 1 1
      src/Database/Type/UuidType.php
  2. 19 0
      tests/TestCase/ORM/TableUuidTest.php

+ 1 - 1
src/Database/Type/UuidType.php

@@ -45,7 +45,7 @@ class UuidType extends \Cake\Database\Type {
  * @return mixed
  */
 	public function toDatabase($value, Driver $driver) {
-		if ($value === null) {
+		if ($value === null || $value === '') {
 			return null;
 		}
 		return strval($value);

+ 19 - 0
tests/TestCase/ORM/TableUuidTest.php

@@ -114,4 +114,23 @@ class TableUuidTest extends TestCase {
 		$this->assertCount(0, $query->execute(), 'No rows left');
 	}
 
+/**
+ * Tests that sql server does not error when an empty uuid is bound
+ *
+ * @return void
+ */
+	public function testEmptyUuid() {
+		$this->skipIf(
+			!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
+			'Empty UUIDs only affect SQLServer uniqueidentifier field types'
+		);
+		$id = '';
+		$table = TableRegistry::get('uuiditems');
+		$entity = $table->find('all')
+			->where(['id' => $id])
+			->first();
+
+		$this->assertNull($entity);
+	}
+
 }