Browse Source

Add isNullable to Schema\Table.

This makes Schema\Table easier to use as callers don't need to know the
internal array structure.
Mark Story 11 years ago
parent
commit
fe747324b6
2 changed files with 38 additions and 0 deletions
  1. 16 0
      src/Database/Schema/Table.php
  2. 22 0
      tests/TestCase/Database/Schema/TableTest.php

+ 16 - 0
src/Database/Schema/Table.php

@@ -342,6 +342,22 @@ class Table
     }
 
     /**
+     * Check whether or not a field is nullable
+     *
+     * Missing columns are nullable.
+     *
+     * @param string $name The column to get the type of.
+     * @return bool Whether or not the field is nullable.
+     */
+    public function isNullable($name)
+    {
+        if (!isset($this->_columns[$name])) {
+            return true;
+        }
+        return ($this->_columns[$name]['null'] === true);
+    }
+
+    /**
      * Get a hash of columns and their default values.
      *
      * @return array

+ 22 - 0
tests/TestCase/Database/Schema/TableTest.php

@@ -66,6 +66,28 @@ class TableTest extends TestCase
     }
 
     /**
+     * Test isNullable method
+     *
+     * @return void
+     */
+    public function testIsNullable()
+    {
+        $table = new Table('articles');
+        $table->addColumn('title', [
+            'type' => 'string',
+            'length' => 25,
+            'null' => false
+        ])->addColumn('tagline', [
+            'type' => 'string',
+            'length' => 25,
+            'null' => true
+        ]);
+        $this->assertFalse($table->isNullable('title'));
+        $this->assertTrue($table->isNullable('tagline'));
+        $this->assertTrue($table->isNullable('missing'));
+    }
+
+    /**
      * Test columnType method
      *
      * @return void