浏览代码

Merge pull request #5455 from cakephp/issue-5454

Fix null default values not being returned by defaultValues()
José Lorenzo Rodríguez 11 年之前
父节点
当前提交
adf8f2c743
共有 2 个文件被更改,包括 14 次插入3 次删除
  1. 6 2
      src/Database/Schema/Table.php
  2. 8 1
      tests/TestCase/Database/Schema/TableTest.php

+ 6 - 2
src/Database/Schema/Table.php

@@ -342,9 +342,13 @@ class Table {
 	public function defaultValues() {
 		$defaults = [];
 		foreach ($this->_columns as $name => $data) {
-			if (isset($data['default'])) {
-				$defaults[$name] = $data['default'];
+			if (!array_key_exists('default', $data)) {
+				continue;
 			}
+			if ($data['default'] === null && $data['null'] !== true) {
+				continue;
+			}
+			$defaults[$name] = $data['default'];
 		}
 		return $defaults;
 	}

+ 8 - 1
tests/TestCase/Database/Schema/TableTest.php

@@ -162,13 +162,20 @@ class TableTest extends TestCase {
 		])->addColumn('title', [
 			'type' => 'string',
 			'default' => 'A title'
+		])->addColumn('name', [
+			'type' => 'string',
+			'null' => false,
+			'default' => null,
 		])->addColumn('body', [
 			'type' => 'text',
+			'null' => true,
+			'default' => null,
 		]);
 		$result = $table->defaultValues();
 		$expected = [
 			'id' => 0,
-			'title' => 'A title'
+			'title' => 'A title',
+			'body' => null
 		];
 		$this->assertEquals($expected, $result);
 	}